Sitemap
Prince Kumar Chaurasia

Prince Kumar Chaurasia

Quick Sort using Recursion

--

“Quick Sort: Pick a pivot, split and sort — speed meets elegance in the art of ordering.” ⚡🚀

Press enter or click to view image in full size

After understanding MERGE SORT in the previous article, let’s move on to another sorting algorithm → QUICK SORT.

Like merge sort, we will use recursion to implement quick sort. But first let’s understand the basic idea about it.

Basic Idea

The basic idea behind quick sort is to pick a pivot and place it at its correct position such that all the elements on the left are smaller and those on right are greater than the pivot.

Code Implementation

    private static void quickSort(int[] arr, int low, int high) {
if(low >= high){
return;
}

int start = low, end = high, mid = start + ( end - start ) / 2, pivot = arr[mid];

while (start <= end){
while (arr[start] < pivot){
start++;
}

while (arr[end] > pivot){
end--;
}

if(start <= end){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

quickSort(arr, low, end);
quickSort(arr, start, high);

}

In the quickSort function we take the array to be sorted, low [ which is first index of the array ] and hight [ which is end of the array ].

Then, we define the base case, i.e., when the low crosses or is equal to high, return.

Further we initialise start, end, mid and pivot variables. The start begins at the low index, end at the high index, and mid is calculated as the middle element. We choose the element at the mid position as our pivot.

Get Prince Kumar Chaurasia’s stories in your inbox

Join Medium for free to get updates from this writer.

The main sorting happens in the while loop that runs until start and end cross each other:

  1. The first inner while loop finds elements that are greater than or equal to the pivot from the left side.
  2. The second inner while loop finds elements that are less than or equal to the pivot from the right side.
  3. When both loops find elements that are out of place (a larger element on the left and a smaller element on the right), we swap them.
  4. After swapping, we increment start and decrement end to continue the process.

Once the outer while loop completes, we have partitioned the array around the pivot. Elements smaller than the pivot are on the left, and elements greater than the pivot are on the right.

Now, we recursively apply quickSort on the two partitions:

  1. From low to end (left partition)
  2. From start to high (right partition)

Let’s analyze the time complexity:

  • Best and Average case: O(n log n)
  • Worst case: O(n²) — occurs when the array is already sorted or when we always pick the smallest/largest element as pivot

Space complexity is O(log n) for the recursion stack in the average case, and O(n) in the worst case.

Quick Sort is generally faster than other O(n log n) sorting algorithms like Merge Sort because it has better locality of reference and doesn’t require additional space for merging. However, it’s not stable (doesn’t preserve the relative order of equal elements) and its worst-case performance can be problematic for certain inputs.

In this way, you can sort an unsorted array using Quick Sort algorithm.

Thanks for reading this far, hit applaud if you find it helpful understanding the algorithm.
See you in the next one.

BYE 👋

--

--