CoursifyCoursify

Quick Sort Algorithm and Its Best-, Worst-, and Average-Case Time Complexities

Quick Sort Algorithm and Its Best-, Worst-, and Average-Case Time Complexities

Verified Sources
May 26, 2026

Quick sort is a divide-and-conquer comparison sort that selects a pivot and rearranges the array so that elements smaller than the pivot come before it and larger elements come after it. The two resulting subarrays are then sorted recursively.2

Quick sort is widely studied because its expected running time is O(nlogn)O(n \log n), while its worst-case running time is O(n2)O(n^2). The difference comes entirely from how balanced the partitions are after each pivot selection.2

A general recurrence for quick sort is:

T(n)=T(k)+T(nk1)+Θ(n)T(n)=T(k)+T(n-k-1)+\Theta(n)

where kk is the number of elements placed to the left of the pivot after partitioning, and the Θ(n)\Theta(n) term comes from the linear-time partition step.2

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. 2

  2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. 2

  3. Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to O(n2)O(n^2) on sorted input.

  4. CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case.

Quicksort algorithm

Core Insight

Quick sort does not merge subarrays like merge sort. After partitioning, the pivot is already in its final position, so the algorithm only needs recursive calls on the two sides.2

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

  2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

Algorithm Idea

The most common classroom version uses the last element as the pivot and a partition routine such as the Lomuto partition scheme.2 The partition operation scans the subarray once, compares each element with the pivot, and swaps elements to ensure the final pivot position is correct. Since this scan is linear, partition takes Θ(n)\Theta(n) time for a subarray of size nn.2

If the pivot splits the array nearly equally every time, the recursion depth is about log2n\log_2 n, and each level of the recursion tree performs total work proportional to nn. Hence the total time becomes O(nlogn)O(n \log n). If the pivot is always the smallest or largest element, the recursion becomes highly unbalanced and degrades to O(n2)O(n^2).3

Footnotes

  1. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. 2 3

  2. Quick Sort - GeeksforGeeks - Standard educational presentation of quick sort and example execution.

  3. CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case.

  4. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

  5. Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to O(n2)O(n^2) on sorted input.

Quick Sort Algorithm (with Partition)

  1. 1
    Step 1

    If the subarray has zero or one element, it is already sorted, so return immediately.2

    Footnotes

    1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

    2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

  2. 2
    Step 2

    Select a pivot element. In a simple implementation, the last element of the subarray is chosen.2

    Footnotes

    1. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

    2. Quick Sort - GeeksforGeeks - Standard educational presentation of quick sort and example execution.

  3. 3
    Step 3

    Rearrange elements so that all values less than or equal to the pivot appear before it, and all larger values appear after it. After partitioning, the pivot reaches its final sorted index.2

    Footnotes

    1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

    2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

  4. 4
    Step 4

    Apply quick sort to the subarray to the left of the pivot index.

    Footnotes

    1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

  5. 5
    Step 5

    Apply quick sort to the subarray to the right of the pivot index.

    Footnotes

    1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

  6. 6
    Step 6

    When all recursive calls reach subarrays of size 0 or 1, the full array is sorted.2

    Footnotes

    1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

    2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

Pseudocode

A standard quick sort algorithm can be written as follows:2

1QUICKSORT(A, low, high) 2 if low < high 3 p = PARTITION(A, low, high) 4 QUICKSORT(A, low, p - 1) 5 QUICKSORT(A, p + 1, high) 6 7PARTITION(A, low, high) 8 pivot = A[high] 9 i = low - 1 10 11 for j = low to high - 1 12 if A[j] <= pivot 13 i = i + 1 14 swap A[i] and A[j] 15 16 swap A[i + 1] and A[high] 17 return i + 1

This version sorts in-place except for recursion stack usage, which is one reason quick sort is popular in practice.2

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. 2

  2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

  3. Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to O(n2)O(n^2) on sorted input.

Why Worst Case Happens

If the pivot is repeatedly chosen as the smallest or largest element, the partitions become sizes 0 and n-1. This creates a linear recursion chain and leads to quadratic time, especially in naive implementations on already sorted input.2

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

  2. Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to O(n2)O(n^2) on sorted input.

Example Walkthrough

Consider the array:

[10,7,8,9,1,5][10, 7, 8, 9, 1, 5]

Using the last element 55 as the pivot, partitioning places all elements 5\le 5 to its left and all larger elements to its right. One possible result after the first partition is:

[1,5,8,9,10,7][1, 5, 8, 9, 10, 7]

Now the pivot 55 is in its final sorted position. Quick sort recursively processes the left subarray [1][1] and the right subarray [8,9,10,7][8, 9, 10, 7]. Repeating this process eventually sorts the full array.2

Footnotes

  1. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

  2. Quick Sort - GeeksforGeeks - Standard educational presentation of quick sort and example execution.

Time Complexity Analysis

The running time depends on the partition sizes produced at each recursive call.2

1. Best Case

The best case occurs when each pivot divides the array into two nearly equal halves.2

The recurrence is:

T(n)=2T(n2)+Θ(n)T(n)=2T\left(\frac{n}{2}\right)+\Theta(n)

By the Master Theorem or recursion-tree reasoning:

T(n)=O(nlogn)T(n)=O(n \log n)

Reason:

  • There are about log2n\log_2 n levels of recursion.
  • Each level performs total partition work of Θ(n)\Theta(n).2

So:

Best-case time complexity=O(nlogn)\text{Best-case time complexity} = O(n \log n)

2. Worst Case

The worst case occurs when the pivot is always the smallest or largest element, producing partitions of sizes 00 and n1n-1.2

The recurrence is:

T(n)=T(n1)+Θ(n)T(n)=T(n-1)+\Theta(n)

Expanding:

T(n)=Θ(n)+Θ(n1)+Θ(n2)++Θ(1)T(n)=\Theta(n)+\Theta(n-1)+\Theta(n-2)+\cdots+\Theta(1)

Thus:

T(n)=O(n2)T(n)=O(n^2)

So:

Worst-case time complexity=O(n2)\text{Worst-case time complexity} = O(n^2)

3. Average Case

The average case assumes pivot positions are reasonably distributed over recursive calls. Mathematical analyses show that the expected number of comparisons is proportional to nlognn \log n.2

A representative recurrence is:

T(n)=T(k)+T(nk1)+Θ(n)T(n)=T(k)+T(n-k-1)+\Theta(n)

averaged over all possible pivot positions k=0,1,,n1k=0,1,\dots,n-1. This yields:

T(n)=O(nlogn)T(n)=O(n \log n)

So:

Average-case time complexity=O(nlogn)\text{Average-case time complexity} = O(n \log n)

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. 2 3 4 5 6

  2. CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case. 2 3

  3. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. 2

  4. Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to O(n2)O(n^2) on sorted input.

Quick Sort Time Complexity Comparison

Relative asymptotic growth across cases

CaseRecurrenceTime Complexity
BestT(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)O(nlogn)O(n \log n)
AverageT(n)=T(k)+T(nk1)+Θ(n)T(n)=T(k)+T(n-k-1)+\Theta(n)O(nlogn)O(n \log n)
WorstT(n)=T(n1)+Θ(n)T(n)=T(n-1)+\Theta(n)O(n2)O(n^2)

Logical Flow of Quick Sort Execution

Pivot Selection

Stage 1

Choose a pivot from the current subarray.2"

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

  2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

Partition

Stage 2

Move smaller elements left and larger elements right in linear time.2"

Footnotes

  1. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

  2. CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case.

Recursive Reduction

Stage 3

Apply the same process to left and right subarrays until size 0 or 1."

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

Termination

Stage 4

Once all subproblems are trivially sorted, the array is fully ordered.2"

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis.

  2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

Important Notes and Exam-Focused Clarifications

Final Answer

The algorithm for quick sort is:

1QUICKSORT(A, low, high) 2 if low < high 3 p = PARTITION(A, low, high) 4 QUICKSORT(A, low, p - 1) 5 QUICKSORT(A, p + 1, high)

with partition:

1PARTITION(A, low, high) 2 pivot = A[high] 3 i = low - 1 4 for j = low to high - 1 5 if A[j] <= pivot 6 i = i + 1 7 swap A[i], A[j] 8 swap A[i + 1], A[high] 9 return i + 1

Its time complexities are:

  • Best case: O(nlogn)O(n \log n) when the pivot divides the array into nearly equal halves.2
  • Average case: O(nlogn)O(n \log n) in the expected sense over pivot positions or input permutations.2
  • Worst case: O(n2)O(n^2) when the pivot repeatedly produces splits of sizes 00 and n1n-1.2

Thus, the standard result is:

Best=O(nlogn),Average=O(nlogn),Worst=O(n2)\boxed{\text{Best} = O(n \log n),\quad \text{Average} = O(n \log n),\quad \text{Worst} = O(n^2)}

Footnotes

  1. Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. 2 3

  2. Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis.

  3. CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case.

  4. Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to O(n2)O(n^2) on sorted input.

Knowledge Check

Question 1 of 5
Q1Single choice

What is the main idea behind quick sort?

Explore Related Topics

1

CPU Scheduling Case Study: FCFS vs SJF for a Five-Process Workload

The case study compares FCFS and non‑preemptive SJF scheduling for five processes that all arrive at time 0, showing their Gantt charts, individual waiting times, and average waiting times.

  • FCFS order A → B → C → D → E; waiting times 0, 10, 11, 13, 14 ms; average 9.69.6 ms.
  • SJF order B → D → C → E → A (ties broken by arrival order); waiting times 0, 1, 2, 4, 9 ms; average 3.23.2 ms.
  • With simultaneous arrivals, each process’s waiting time equals the sum of burst times of all jobs scheduled before it.
  • Non‑preemptive SJF is optimal for minimizing average waiting time when burst lengths are known.
  • The priority column is irrelevant for this comparison.
2

Solving the 0/1 Knapsack Problem: Brute Force, Greedy, Dynamic Programming, and Branch-and-Bound

The 0/1 knapsack problem—selecting whole items to maximize value under capacity WW—is examined through four classic solution strategies: brute‑force, greedy, dynamic programming, and branch‑and‑bound.

  • Brute force checks all 2n2^n subsets, guaranteeing optimality but with exponential O(2n)O(2^n) time.
  • Greedy heuristics (e.g., highest vi/wiv_i/w_i first) run in O(nlogn)O(n\log n) but can miss the optimum because 0/1 knapsack lacks the greedy‑choice property.
  • Dynamic programming exploits optimal substructure, solving in O(nW)O(nW) time and O(nW)O(nW) (or O(W)O(W)) space, yet is pseudo‑polynomial and costly for large WW.
  • Branch‑and‑bound explores a decision tree, pruning nodes via fractional‑knapsack upper bounds; worst‑case O(2n)O(2^n) but often far faster on favorable instances.
Chat with Kiro