Implementing Quicksort Algorithm in Java

Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.

The steps are:

  1. Pick an element, called a pivot, from the array.
  2. Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
  3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.

(Via Wikipedia)

import java.lang.*;
import java.io.*;

class QuickSort {
    public static void display(int x[]) throws ArrayIndexOutOfBoundsException {
        for (int i = 0; i < x.length; i++) {
            System.out.print(x[i] + "\t");
        }
    }

    public static int partition(int x[], int l, int r) throws ArrayIndexOutOfBoundsException {
        int i, j, t, y;
        i = l;
        j = r;
        y = x[l];

        while (i < j) {
            while (x[i] <= y && i <= r)
                i++;
            while (x[j] > y)
                j--;
            if (i < j) {
                t = x[i];
                x[i] = x[j];
                x[j] = t;
            }
        }
        t = x[j];
        x[j] = x[l];
        x[l] = t;
        return j;
    }
    public static void qckSort(int x[], int l, int r) throws ArrayIndexOutOfBoundsException {
        int p;
        if (l < r) {
            p = partition(x, l, r);
            qckSort(x, l, p - 1);
            qckSort(x, p + 1, r);
        }
    }

    public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\nEnter number of values you want to enter : ");
        String str = br.readLine();
        int n = Integer.parseInt(str);
        int x[] = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.print("\nEnter number " + (i + 1) + " : ");
            str = br.readLine();
            x[i] = Integer.parseInt(str);
        }
        System.out.println("\nThe entered numbers are:-\t");
        display(x);
        qckSort(x, 0, n - 1);
        System.out.println("\n\nSorted Numbers are:-\t");
        display(x);

    }
}


/*
///////////////////////////////////OUTPUT//////////////////////////////

Enter number of values you want to enter :-     9

Enter numbers:-
15
48
66
12
48
74
12
24
56

The entered numbers are:-
15      48      66      12      48      74      12      24      56

Sorted Numbers are:-
12      12      15      24      48      48      56      66      74
process exit.......		
*/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.