1. Home
  2. Explore
  3. Computer Science
  4. Quicksort Algorithm

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Quicksort Algorithm

🚀 Quicksort Algorithm Explained

Quicksort is an efficient, comparison-based sorting algorithm that employs a divide-and-conquer strategy. Unlike Merge Sort, which divides the list into two equal halves, Quicksort partitions the list based on a pivot element, placing smaller elements on one side and larger elements on the other. This process is recursively applied to the sub-lists until the entire list is sorted.

🎯 How Quicksort Works: The Partitioning Process

The core idea of Quicksort revolves around selecting a pivot and partitioning the array around it.

  1. Pivot Selection:

    • Choose one element from the list to be the pivot.

    • Common choices include the first, last, or a random element to simplify implementation.

    • The choice of pivot significantly impacts the algorithm's performance, as we will see.

  2. Partitioning:

    • Rearrange the list such that all elements less than or equal to the pivot are placed on one side (e.g., left), and all elements greater than the pivot are placed on the other side (e.g., right).

    • The pivot itself ends up in its final sorted position.

  3. Recursive Sorting:

    • Recursively apply the Quicksort algorithm to the two sub-lists created by the partition (the left sub-list and the right sub-list).

    • This continues until each sub-list contains zero or one element, at which point they are considered sorted.

📝 Example Walkthrough

Let's illustrate with an unsorted list: [7, 8, 7, 4, 3, 10, 5]

  1. Initial Step:

    • Select Pivot: Choose the rightmost element, 5, as the pivot.

    • Partition:

      • Elements ≤5\le 5≤5: [4, 3]

      • Elements >5> 5>5: [7, 8, 7, 10]

    • Resulting structure: [4, 3] | 5 | [7, 8, 7, 10]

  2. Recursive Call on Left Sub-list [4, 3]:

    • Select Pivot: Choose the rightmost element, 3, as the pivot.

    • Partition:

      • Elements ≤3\le 3≤3: []

      • Elements >3> 3>3: [4]

    • Resulting structure: [] | 3 | [4]

    • Recursively sort [4]: It's a single element, so it's sorted.

  3. Recursive Call on Right Sub-list [7, 8, 7, 10]:

    • Select Pivot: Choose the rightmost element, 10, as the pivot.

    • Partition:

      • Elements ≤10\le 10≤10: [7, 8, 7]

      • Elements >10> 10>10: []

    • Resulting structure: [7, 8, 7] | 10 | []

    • Recursively sort [7, 8, 7]:

      • Select Pivot: Choose the rightmost element, 7, as the pivot.

      • Partition:

        • Elements ≤7\le 7≤7: [7]

        • Elements >7> 7>7: [8]

      • Resulting structure: [7] | 7 | [8]

      • Recursively sort [7] and [8] (single elements, so sorted).

🔄 Rejoining (Conquering) the Lists

Once all sub-lists are reduced to single elements, they are inherently sorted. The "rejoining" phase is implicit in how the partitioning places the pivot in its final position.

  • The elements on the left are already sorted among themselves and are smaller than the pivot.

  • The elements on the right are already sorted among themselves and are larger than the pivot.

  • When the recursion unwinds, the sorted sub-lists combine with their pivots to form larger sorted lists.

For example, [7] | 7 | [8] becomes [7, 7, 8]. Then, [7, 7, 8] | 10 | [] becomes [7, 7, 8, 10]. Similarly, [] | 3 | [4] becomes [3, 4]. Finally, [3, 4] | 5 | [7, 7, 8, 10] combines to form the fully sorted list: [3, 4, 5, 7, 7, 8, 10].


⚖️ Performance Analysis: Best, Average, and Worst Cases

The efficiency of Quicksort is highly dependent on the pivot selection strategy.

🌟 Best and Average Case Performance

  • Ideal Pivot: The best scenario occurs when the pivot consistently divides the list into two roughly equal halves. This resembles the behavior of Merge Sort.

  • Average Case: In the average case, Quicksort performs very well.

    • Time Complexity: O(Nlog⁡N)O(N \log N)O(NlogN)

    • This is comparable to Merge Sort's performance.

📉 Worst Case Performance

The worst-case scenario arises when the pivot selection consistently results in highly unbalanced partitions.

  • Scenario: This happens if the pivot is always the smallest or largest element in the sub-list. For example, if the list is already sorted, and we always pick the rightmost element as the pivot.

  • Example (Already Sorted List): [3, 4, 5, 7, 7, 8, 10]

    1. Pick 10 as pivot. All other elements [3, 4, 5, 7, 7, 8] go to the left.

    2. Pick 8 as pivot from [3, 4, 5, 7, 7, 8]. All [3, 4, 5, 7, 7] go to the left.

    3. This continues, creating one sub-list of N-1 elements and one empty sub-list.

    4. The recursion depth becomes NNN, and each step involves partitioning NNN elements.

  • Worst Case Time Complexity: O(N2)O(N^2)O(N2)

    • This is equivalent to the performance of Bubble Sort in its worst case.

    • It means the number of operations grows with the square of the input size, which is highly inefficient for large datasets.

📊 Comparison with Other Sorting Algorithms

Algorithm

Best Case Time Complexity

Average Case Time Complexity

Worst Case Time Complexity

Quicksort

O(Nlog⁡N)O(N \log N)O(NlogN)

O(Nlog⁡N)O(N \log N)O(NlogN)

O(N2)O(N^2)O(N2)

Merge Sort

O(Nlog⁡N)O(N \log N)O(NlogN)

O(Nlog⁡N)O(N \log N)O(NlogN)

O(Nlog⁡N)O(N \log N)O(NlogN)

Bubble Sort

O(N)O(N)O(N)

O(N2)O(N^2)O(N2)

O(N2)O(N^2)O(N2)

Key takeaway: While Quicksort is generally faster in practice due to lower constant factors (especially for in-place implementations), its worst-case O(N2)O(N^2)O(N2) performance is a significant drawback compared to Merge Sort's consistent O(Nlog⁡N)O(N \log N)O(NlogN). Implementations often use techniques like randomized pivot selection or median-of-three pivot selection to mitigate the risk of hitting the worst-case scenario.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent