Sorting means arranging data in a defined order so that it can be searched, merged and displayed easily. Quick sort is one of the fastest general-purpose sorting algorithms in practice and the basis of many library sorts, including Java's Arrays.sort() for primitive arrays (a dual-pivot variant).
What is Quick Sort?
Quick sort is a divide-and-conquer algorithm invented by Tony Hoare in 1959 (published in 1961). It works in three steps:
- Choose a pivot, one element of the current part of the array.
- Partition: rearrange the part so that every element smaller than the pivot comes before it and every other element comes after it. The pivot is now in its final sorted position.
- Recurse on the part to the left of the pivot and on the part to the right of it. Parts with 0 or 1 element are already sorted.
Unlike merge sort, quick sort needs no merging step and no extra array: all the work happens during partitioning, inside the original array.
Dry run of one partition of [8, 3, 5, 9, 1] using the middle element 5 as pivot:
move pivot to the end: [8, 3, 1, 9, 5] scan, moving values < 5 to the front: 3 < 5 -> [3, 8, 1, 9, 5] 1 < 5 -> [3, 1, 8, 9, 5] put pivot after them: [3, 1, 5, 9, 8] (5 is in its final place) recurse on [3, 1] and on [9, 8]
Example of Quick Sort in Java
The program uses the Lomuto partition scheme with the middle element as pivot, which avoids the classic worst case on already-sorted input. It also recurses only into the smaller part and loops on the larger one, so the recursion depth stays O(log n).
// Quick sort: choose a pivot, move smaller values to its left and larger
// values to its right, then sort the two sides the same way
public class QuickSortExample {
static void quickSort(int[] array, int low, int high) {
// Recurse into the smaller part and loop on the larger one,
// so the recursion depth stays O(log n) even for bad pivots
while (low < high) {
int p = partition(array, low, high);
if (p - low < high - p) {
quickSort(array, low, p - 1);
low = p + 1;
} else {
quickSort(array, p + 1, high);
high = p - 1;
}
}
}
// Lomuto partition with the middle element as pivot.
// Returns the pivot's final index; everything left of it is smaller,
// everything right of it is greater or equal.
static int partition(int[] array, int low, int high) {
int middle = low + (high - low) / 2;
swap(array, middle, high); // move the pivot to the end
int pivot = array[high];
int boundary = low; // next slot for a value smaller than the pivot
for (int j = low; j < high; j++) {
if (array[j] < pivot) {
swap(array, boundary, j);
boundary++;
}
}
swap(array, boundary, high); // put the pivot between the two parts
return boundary;
}
static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
static void printArray(int[] array) {
StringBuilder line = new StringBuilder();
for (int value : array) {
line.append(value).append(' ');
}
System.out.println(line.toString().trim());
}
public static void main(String[] args) {
int[] array = {40, 70, 90, 20, 80, 100, 60, 10, 30, 50};
quickSort(array, 0, array.length - 1);
System.out.println("Sorted Array:");
printArray(array);
}
}
Sorted Array: 10 20 30 40 50 60 70 80 90 100
Time and Space Complexity
| Case | Time | When it happens |
|---|---|---|
| Best | O(n log n) | Every pivot splits its part into two halves of about the same size. |
| Average | O(n log n) | Random input or a randomly chosen pivot. |
| Worst | O(n2) | The pivot is always the smallest or largest value, for example when the first or last element is used as pivot on an already sorted array, or when all values are equal with a two-way partition. |
Space: quick sort sorts in place; the only extra memory is the recursion stack: O(log n) on average, and O(log n) in the worst case too when, as above, the code recurses into the smaller part first (a naive version can need O(n)).
Stability: quick sort is not stable; partitioning can swap equal elements past each other.
When to Use Quick Sort
- Good for: large arrays of primitive values in memory, where its small constant factors and cache-friendly access make it very fast.
- Protect against the worst case with a random or median-of-three pivot, and use three-way partitioning when there are many duplicate values.
- Avoid it when a stable sort or a guaranteed O(n log n) bound is required; use merge sort (or Java's TimSort for objects) instead.