Quick Sort Algorithm and Its Best-, Worst-, and Average-Case Time Complexities
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 , while its worst-case running time is . The difference comes entirely from how balanced the partitions are after each pivot selection.2
A general recurrence for quick sort is:
where is the number of elements placed to the left of the pivot after partitioning, and the term comes from the linear-time partition step.2
Footnotes
-
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
-
Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to on sorted input. ↩
-
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
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
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 time for a subarray of size .2
If the pivot splits the array nearly equally every time, the recursion depth is about , and each level of the recursion tree performs total work proportional to . Hence the total time becomes . If the pivot is always the smallest or largest element, the recursion becomes highly unbalanced and degrades to .3
Footnotes
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩ ↩2 ↩3
-
Quick Sort - GeeksforGeeks - Standard educational presentation of quick sort and example execution. ↩
-
CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case. ↩
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to on sorted input. ↩
Quick Sort Algorithm (with Partition)
- 1Step 1
If the subarray has zero or one element, it is already sorted, so return immediately.2
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩
-
- 2Step 2
Select a pivot element. In a simple implementation, the last element of the subarray is chosen.2
Footnotes
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩
-
Quick Sort - GeeksforGeeks - Standard educational presentation of quick sort and example execution. ↩
-
- 3Step 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
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩
-
- 4Step 4
Apply quick sort to the subarray to the left of the pivot index.
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
- 5Step 5
Apply quick sort to the subarray to the right of the pivot index.
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
- 6Step 6
When all recursive calls reach subarrays of size 0 or 1, the full array is sorted.2
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
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
-
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. ↩
-
Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to 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
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to on sorted input. ↩
Example Walkthrough
Consider the array:
Using the last element as the pivot, partitioning places all elements to its left and all larger elements to its right. One possible result after the first partition is:
Now the pivot is in its final sorted position. Quick sort recursively processes the left subarray and the right subarray . Repeating this process eventually sorts the full array.2
Footnotes
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩
-
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:
By the Master Theorem or recursion-tree reasoning:
Reason:
- There are about levels of recursion.
- Each level performs total partition work of .2
So:
2. Worst Case
The worst case occurs when the pivot is always the smallest or largest element, producing partitions of sizes and .2
The recurrence is:
Expanding:
Thus:
So:
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 .2
A representative recurrence is:
averaged over all possible pivot positions . This yields:
So:
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩ ↩2 ↩3 ↩4 ↩5 ↩6
-
CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case. ↩ ↩2 ↩3
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩ ↩2
-
Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to on sorted input. ↩
Quick Sort Time Complexity Comparison
Relative asymptotic growth across cases
| Case | Recurrence | Time Complexity |
|---|---|---|
| Best | ||
| Average | ||
| Worst |
Logical Flow of Quick Sort Execution
Pivot Selection
Stage 1Choose a pivot from the current subarray.2"
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩
Partition
Stage 2Move smaller elements left and larger elements right in linear time.2"
Footnotes
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩
-
CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case. ↩
Recursive Reduction
Stage 3Apply the same process to left and right subarrays until size 0 or 1."
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
Termination
Stage 4Once all subproblems are trivially sorted, the array is fully ordered.2"
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩
-
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: when the pivot divides the array into nearly equal halves.2
- Average case: in the expected sense over pivot positions or input permutations.2
- Worst case: when the pivot repeatedly produces splits of sizes and .2
Thus, the standard result is:
Footnotes
-
Quicksort - Wikipedia - Overview of quicksort, partitioning idea, and best/average/worst-case analysis. ↩ ↩2 ↩3
-
Quick Sort Algorithm - EnjoyAlgorithms - Explains quick sort pseudocode, partition cost, and recurrence-based analysis. ↩
-
CMSC 351: QuickSort (UMD PDF) - Formal recurrence relations for partition-based quicksort analysis, including average case. ↩
-
Sorting algorithm - Wikipedia - Notes that naive pivot choice can lead quicksort to on sorted input. ↩
Knowledge Check
What is the main idea behind quick sort?
Explore Related Topics
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 ms.
- SJF order B → D → C → E → A (ties broken by arrival order); waiting times 0, 1, 2, 4, 9 ms; average 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.
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 —is examined through four classic solution strategies: brute‑force, greedy, dynamic programming, and branch‑and‑bound.
- Brute force checks all subsets, guaranteeing optimality but with exponential time.
- Greedy heuristics (e.g., highest first) run in but can miss the optimum because 0/1 knapsack lacks the greedy‑choice property.
- Dynamic programming exploits optimal substructure, solving in time and (or ) space, yet is pseudo‑polynomial and costly for large .
- Branch‑and‑bound explores a decision tree, pruning nodes via fractional‑knapsack upper bounds; worst‑case but often far faster on favorable instances.
