Quick Sort (OCR A-Level Computer Science): Revision Notes
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
Example: Given the array 8, 3, 7, 4, 6, 2, 5, 1
- Initial Array: 8, 3, 7, 4, 6, 2, 5, 1
- Choose pivot: 1
- Partition: 1, 3, 7, 4, 6, 2, 5, 8
- Left Sub-array: 1 (sorted)
- Right Sub-array: 3, 7, 4, 6, 2, 5, 8
- Choose pivot: 8
- Partition: 3, 7, 4, 6, 2, 5, 8
- 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
Common Mistakes
- Choosing Poor Pivot: Always picking the first or last element in an already sorted array can degrade performance to O(n²).
- Incorrect Partitioning: Failing to correctly manage indices (
iandj) can result in incorrect placement of elements. - Not Handling Base Case in Recursion: Forgetting to stop recursion when
low >= highleads to infinite recursion. - Ignoring In-Place Sorting Requirement: Some implementations mistakenly use extra space, which defeats the purpose of in-place sorting.
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.