How to Implement Quick Sort in C?

Quick Sort is an efficient sorting algorithm developed by Tony Hoare in 1959. It is still a commonly used sorting algorithm in most practical cases. If implemented properly, it is two or three times faster than other efficient sorting algorithms like merge sort or heap sort. Here we’ll see how to implement this sorting algorithm in C programming language. And also we’ll discuss about the algorithm and analyze the performance in various conditions.

Quick Sort Algorithm

Quick sort is a divide and conquer algorithm which is generally implemented using recursive function.

  1. Select an element from the array as pivot.
  2. Partition the array. One partition will contain all the elements that are less than the pivot. Another partition will contain all elements that are greater than the pivot.
  3. Apply this procedure recursively with these two partitions.

Recursion will stop when a partition will have 1 or 0 element. This is the base case of the recursion. Performance of merge sort is heavily dependent on the selection of the pivot. Different strategies are taken based on the condition to select the pivot to get better performance.

  1. First element of the partition is selected as pivot.
  2. Last Element of the partition is selected as pivot.
  3. Random element of the partition is selected as pivot.
  4. Median of first, middle and last element of the partition is selected as pivot.

Graphical Illustration of Quick Sort

quick sort graphical illustration

The diagram above shows how quick sort works. First one element is selected as pivot. In this case 4 is selected as pivot in the first iteration. The array is than sub-divided into two partitions. Left partition contains all the element which are less than the pivot 4 and the right partition contains all the elements which are greater than 4. If we continue this procedure after 2 more iterations, the array will become sorted.

Implementation of Quick Sort in C

Here is the complete C program that implements the quick sort.

 

bbbb

Performance of Quick Sort

Worst Case: Running time of quick sort in worst case scenario is O(n2). Worst case scenario occurs when pivot divides two partitions of size 0 and n-1, most unbalanced partition. In every iteration one partition would not have any element and other partition will have remaining n-1 elements. This situation occurs when smallest or biggest element is selected as pivot. One example of such situation is when we apply quicksort on a sorted array and first or last element is selected as pivot.

Best Case: Running time of quick sort in best case scenario is O(n log n). Worst case scenario occurs when pivot divides two partitions of equal number of elements in every iteration. One example of such situation is when we apply quicksort on a sorted array and the middle element is selected as pivot.

Average Case: Running time of quick sort in average case scenario is O(n log n). In average case the partitions may not be equal but average size of the partitions of all iterations will be equal. Average condition occurs when we apply quicksort on a randomly ordered array.

The diagram below shows an experimental result how quicksort performs in various conditions.

quick sort performance

We can see that the actual number of comparisons in worst case is much higher than the best or average case. Another thing to notice that the growth rate of number of comparisons with respect to the number of inputs is also much much higher in worst case than best or average case.

Leave a Reply

Your email address will not be published. Required fields are marked *