Summary: In this tutorial, we will learn what the Quicksort algorithm is, how it works, and how to sort an array using the Quicksort algorithm in C, C++, and Java.

Introduction to QuickSort Algorithm

Quicksort is a┬ádivide-and-conquer algorithm. It works by selecting a ‘pivot’ element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted┬árecursively.

Wikipedia

The Quicksort algorithm first selects an element as a pivot, then partitions other elements based on the pivot.

It basically, moves lesser elements to the left sub-array and greater element to the right sub-array.

This process is repeated recursively for the sub-arrays until the sub-array is left with only a single element.

For example, consider the following sorting of an array using Quicksort Algorithm:

Quick Sort with Array

Blue element is the pivot element of the respective sub-array.

After selecting the pivot element we shift other elements accordingly – If less, it is shifted to the left of the pivot otherwise to its right.

The position of the pivot element after the partition is its final correct position in the sorted array.

This process is repeated for every partition (sub-array) i.e. every time we select a pivot element we move it to its correct final position by partitioning and shifting other elements.

Since the process of selecting a pivot element and partitioning others happens on the same array, the array gets sorted in the process.

Quicksort Algorithm

  1. Choose a pivot element i.e. pivot element = array[startIndex].
  2. Shift all the elements to the left of the pivot element which is less than the pivot element.
  3. Again shift all the elements to the right of the pivot element which is greater than the pivot element.
  4. The final index of the pivot element is the dividing point of the array.
  5. Repeat the process (again from step-1) for the subarrays until the sub-array is left with a single element.

Note: On starting we assumed pivot element = array[startIndex]. Here startIndex is the first index of the array or sub-array.

Here is the implementation of the Quicksort Algorithm in C, C++, and Java:

C

C++

Java

Output:


In the above program, the findPivot(...) function is sorting the array as stated in the algorithm and the quickSort(...) function is dividing the array into sub-arrays at index being returned by findPivot(...) function.

Quick Sort Overview:

  • In-place Algorithm: No need for extra arrays.
  • O(nlog2n) time complexity: The array is repeatedly being partitioned into two sub-arrays.
  • Unstable algorithm: Interchanging of duplicate values, which is useless.

Quicksort performs better than Merge sort because it doesn’t require an extra array for the sort process, but is less efficient than Insertion sort.

In this tutorial we learned, what the Quick sort algorithm is, how it works and how to implement Quicksort algorithm in C, C++, and Java to sort an array.

Leave a Reply

16 + 16 =