Photo AI

Last Updated Sep 27, 2025

Quick Sort Simplified Revision Notes

Revision notes with simplified explanations to understand Quick Sort quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

451+ students studying

Quick Sort

Overview

Quick Sort is a highly efficient, comparison-based sorting algorithm that uses the divide-and-conquer strategy. It partitions the array into two sub-arrays around a chosen pivot element such that elements smaller than the pivot go to the left, and elements larger go to the right. This process is recursively applied to each sub-array until the entire list is sorted.

How Quick Sort Works

Step 1: Choose a Pivot

Typically, the first, last, or middle element is chosen, but any element can be the pivot.

Step 2: Partition

Rearrange the array such that:

  • Elements smaller than the pivot are on the left.
  • Elements larger than the pivot are on the right. Step 3: Recursively Apply Quick Sort

Apply the same process to the left and right sub-arrays.

Properties of Quick Sort

  • In-Place: Requires minimal additional memory.
  • Time Complexity:
    • Best and Average Case: O(n log n).
    • Worst Case: O(n²) (occurs when the pivot divides the array poorly, such as always picking the smallest or largest element in an already sorted array).

Steps of Quick Sort

Given an unsorted list:

9, 7, 5, 11, 12, 2, 14, 3, 10, 6

Step 1: Choose a pivot (e.g., last element, 6).

Step 2: Partition the array around the pivot.

5, 2, 3, 6, 12, 11, 14, 9, 10, 7

Step 3: Recursively apply Quick Sort to sub-arrays.

  • Left sub-array: 5, 2, 3
  • Right sub-array: 12, 11, 14, 9, 10, 7 After sorting:

2, 3, 5, 6, 7, 9, 10, 11, 12, 14

Pseudocode for Quick Sort

Quick Sort:

METHOD QuickSort(array, low, high)
    IF low < high
        pivotIndex ← Partition(array, low, high)
        CALL QuickSort(array, low, pivotIndex - 1)
        CALL QuickSort(array, pivotIndex + 1, high)

Partition:

METHOD Partition(array, low, high)
    pivot ← array[high]
    i ← low - 1

    FOR j FROM low TO high - 1
        IF array[j] <= pivot
            i ← i + 1
            SWAP array[i] AND array[j]

    SWAP array[i + 1] AND array[high]
    RETURN i + 1

Python Example

def quick_sort(array, low, high):
    if low < high:
        pivot_index = partition(array, low, high)
        quick_sort(array, low, pivot_index - 1)
        quick_sort(array, pivot_index + 1, high)

def partition(array, low, high):
    pivot = array[high]
    i = low - 1

    for j in range(low, high):
        if array[j] <= pivot:
            i += 1
            array[i], array[j] = array[j], array[i]

    array[i + 1], array[high] = array[high], array[i + 1]
    return i + 1

# Example usage:
data = [9, 7, 5, 11, 12, 2, 14, 3, 10, 6]
quick_sort(data, 0, len(data) - 1)
print(data)  # Output: [2, 3, 5, 6, 7, 9, 10, 11, 12, 14]

Tracing Quick Sort

lightbulbExample

Example: Given the array 8, 3, 7, 4, 6, 2, 5, 1

  1. Initial Array: 8, 3, 7, 4, 6, 2, 5, 1
  • Choose pivot: 1
  • Partition: 1, 3, 7, 4, 6, 2, 5, 8
  1. Left Sub-array: 1 (sorted)
  2. Right Sub-array: 3, 7, 4, 6, 2, 5, 8
  • Choose pivot: 8
  • Partition: 3, 7, 4, 6, 2, 5, 8
  1. Right Sub-array: 3, 7, 4, 6, 2, 5
  • Choose pivot: 5
  • Partition: 3, 4, 2, 5, 6, 7, 8 Repeat until sorted:

1, 2, 3, 4, 5, 6, 7, 8

Note Summary

infoNote

Common Mistakes

  1. Choosing Poor Pivot: Always picking the first or last element in an already sorted array can degrade performance to O(n²).
  2. Incorrect Partitioning: Failing to correctly manage indices (i and j) can result in incorrect placement of elements.
  3. Not Handling Base Case in Recursion: Forgetting to stop recursion when low >= high leads to infinite recursion.
  4. Ignoring In-Place Sorting Requirement: Some implementations mistakenly use extra space, which defeats the purpose of in-place sorting.
infoNote

Key Takeaways

  • Quick Sort is an efficient, in-place sorting algorithm with an average and best-case time complexity of O(n log n).
  • The choice of pivot greatly affects performance. Using strategies like random pivot selection or median-of-three can help improve efficiency.
  • Understanding and correctly implementing the partitioning step is crucial for Quick Sort.
  • While powerful, Quick Sort's worst-case time complexity can degrade to O(n²) if the pivot is poorly chosen.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Quick Sort

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

120 flashcards

Flashcards on Quick Sort

Revise key concepts with interactive flashcards.

Try Computer Science Flashcards

12 quizzes

Quizzes on Quick Sort

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Quick Sort

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Quick Sort

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Quick Sort

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Quick Sort you should explore

Discover More Revision Notes Related to Quick Sort to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Algorithms for the Main Data Structures

Stacks

user avatar
user avatar
user avatar
user avatar
user avatar

230+ studying

183KViews

96%

114 rated

Algorithms for the Main Data Structures

Queues

user avatar
user avatar
user avatar
user avatar
user avatar

273+ studying

186KViews

96%

114 rated

Algorithms for the Main Data Structures

Trees

user avatar
user avatar
user avatar
user avatar
user avatar

382+ studying

193KViews

96%

114 rated

Algorithms for the Main Data Structures

Linked List

user avatar
user avatar
user avatar
user avatar
user avatar

304+ studying

189KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered