Skip to main content

3. Understanding Quick Sort

What is Quick Sort?

Quick Sort is a highly efficient, comparison-based sorting algorithm that also uses a "divide and conquer" strategy. It is one of the most widely used sorting algorithms due to its fast average-case performance. The core idea is to select a 'pivot' element from the array and partition 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.


How Does it Work?

The process can be broken down into three main steps:

  1. Pivot Selection: Choose an element from the array to be the pivot. Common strategies include picking the first element, the last element, the middle element, or a random element.
  2. Partitioning: Reorder the array so that all elements with values less than the pivot come before it, while all elements with values greater than the pivot come after it. After this partitioning, the pivot is in its final sorted position.
  3. Recursion: Recursively apply the above steps to the sub-array of elements with smaller values and the sub-array of elements with greater values. This continues until the base case of an array with zero or one element is reached.

Visualization Explanation

Quicksort-example.gif

  1. The Start: The animation begins with an unsorted list of numbers: [6, 5, 4, 1, 8, 7, 2, 4]. The last element, 4, is chosen as the pivot.

  2. The Partitioning Phase: The goal is to move all numbers smaller than the pivot 4 to its left and all numbers greater than or equal to 4 to its right.

    • The algorithm scans the array. It identifies elements that are smaller than the pivot (1 and 2) and swaps them towards the beginning of the list.
    • After moving all elements smaller than the pivot to the left side, the list might look something like [1, 2, 4, 6, 8, 7, 5, 4].
    • Finally, the pivot element (4 at index 2) is now in its correct sorted position. Everything to its left is smaller, and everything to its right is greater than or equal to it. The pivot 4 is now locked in place.
  3. The "Recursive" Phase: The algorithm now repeats the entire process on the two sub-arrays.

    • Left Sub-array: It runs Quick Sort on [1, 2]. It will pick a new pivot and partition this sub-array.
    • Right Sub-array: It runs Quick Sort on [4, 6, 8, 7, 5, 4]. It will do the same.
    • This recursive splitting and partitioning continues until every sub-array has only one element (or is empty), at which point the entire array is sorted.

Benefits of Quick Sort

  • Fast on Average: It has an average-case time complexity of O(n \log n), which is extremely fast in practice, often outperforming other sorting algorithms.
  • In-place Sorting: It has a space complexity of O(\log n) on average because it sorts the array "in-place," meaning it doesn't require a separate auxiliary array like Merge Sort.
  • Low Overhead: The inner loop of the algorithm is very simple and can be implemented efficiently on most machine architectures.

Drawbacks of Quick Sort

  • Worst-Case Performance: Its main weakness is a worst-case time complexity of O(n^2). This occurs when the chosen pivots are consistently the smallest or largest elements, which can happen with already-sorted or reverse-sorted data.
  • Unstable Sort: Quick Sort is unstable. It does not guarantee that the relative order of equal elements will be preserved after sorting.
  • Sensitive to Pivot Choice: The efficiency of the algorithm is highly dependent on the pivot selection strategy. A