Quick Sort Example Java Program

Definition

Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting.

Quick Sort Example Program

public class QuickSort {
	public static void main(String[] args) {
		int[] arry = { 56,44,78,32,95,06,31 };
		System.out.println("Array before sorting: ");
		for(int i=0;i= high){
			return;
		}
		int middle = low + (high - low) / 2;
		int pivot = arr[middle];
		int i = low, j = high;
		while (i <= j) {
			while (arr[i] < pivot) {
				i++;
			}
			while (arr[j] > pivot) {
				j--;
			}
			if (i <= j) {
				int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
				i++;
				j--;
			}
		}
		if (low < j){
			quickSort(arr, low, j);
		}
		if (high > i){
			quickSort(arr, i, high);
		}
	}
	public static void printArray(int[] arry) {
			System.out.println("After Sorting: ");
		for (int a : arry)
			System.out.println(a + " ");
		System.out.println();
	}
}

Sample Output

Output is:
Array before sorting:
56
44
78
32
95
06
31
After Sorting:
06
31
32
44
56
78
95