In this tutorial, you will learn how quicksort works. Also, you will find working examples of quicksort in Python.

Quicksort is an algorithm based on the divide and conquer approach in which the array is split into subarrays, and these subarrays are recursively called to sort the elements. Quicksort is a sorting algorithm based on the divide and conquer approach, where

  1. An array is divided into subarrays by selecting a pivot element (an element selected from the array). While dividing the array, the pivot element should be positioned in such a way that elements less than the pivot are kept on the left side and elements greater than the pivot are on the right side of the pivot.
  2. The left and right subarrays are also divided using the same approach. This process continues until each subarray contains a single element.
  3. At this point, elements are already sorted. Finally, elements are combined to form a sorted array.
How QuickSort Works?
  1. A pivot element is chosen from the array. You can choose any element from the array as the pivot element.
    Here, we have taken the rightmost (ie, the last element) of the array as the pivot element.
    Quick Sort Steps
    Select a pivot element
  2. The elements smaller than the pivot element are put on the left, and the elements greater than the pivot element are put on the right.
    Quick Sort Steps
    Put all the smaller elements on the left and the greater on the right of the pivot element

    The above arrangement is achieved by the following steps.

    1. A pointer is fixed at the pivot element. The pivot element is compared with the elements beginning from the first index. If the element greater than the pivot element is reached, a second pointer is set for that element.
    2. Now, the pivot element is compared with the other elements (a third pointer). If an element smaller than the pivot element is reached, the smaller element is swapped with the greater element found earlier.
      Quick Sort Steps
      Comparison of the pivot element with other elements
    3. The process goes on until the second-to-last element is reached.
      Finally, the pivot element is swapped with the second pointer.
      Quick Sort Steps
      Swap the pivot element with the second pointer
    4. Now, the left and right subparts of this pivot element are taken for further processing in the steps below.
  3. Pivot elements are again chosen for the left and the right sub-parts separately. Within these sub-parts, the pivot elements are placed in their right position. Then, step 2 is repeated.
    Quick Sort Steps
    Select the pivot element in each half and put in the correct place using recursion
  4. The sub-parts are again divided into smaller sub-parts until each subpart is formed of a single element.
  5. At this point, the array is already sorted.
Quicksort uses recursion for sorting the subparts.

Based on the Divide and conquer approach, the quicksort algorithm can be explained as:

  • Divide
    The array is divided into subparts, taking the pivot as the partitioning point. The elements smaller than the pivot are placed to the left of the pivot and the elements greater than the pivot are placed to the right.
  • Conquer
    The left and the right subparts are again partitioned by selecting pivot elements for them. This can be achieved by recursively passing the subparts into the algorithm.
  • Combine
    This step does not play a significant role in quicksort. The array is already sorted at the end of the conquer step.

You can understand the working of quicksort with the help of the illustrations below.

Quick Sort Steps
Sorting the elements on the left of the pivot using recursion
Quick Sort Steps
sorting the elements on the right of the pivot using recursion
Quick Sort Algorithm
Python, Java, and C/C++ Examples
Quicksort Complexity

Time Complexities

  • Worst Case Complexity [Big-O]: O(n2)
    It occurs when the pivot element picked is either the greatest or the smallest element.
    This condition leads to a case in which the pivot element lies at an extreme end of the sorted array. One sub-array is always empty and another sub-array contains n - 1 elements. Thus, quicksort is called only on this sub-array.
    However, the quick sort algorithm has better performance for scattered pivots

  • Best Case Complexity [Big-omega]: O(n*log n)
    It occurs when the pivot element is always the middle element or near the middle element.
  • Average Case Complexity [Big-theta]: O(n*log n)
    It occurs when the above conditions do not occur.

Space Complexity

The space complexity for quicksort is O(log n).

Quicksort Applications

Quicksort is implemented when

  • The programming language is good for recursion
  • Time complexity matters
  • Space complexity matters

Leave a Comment