QuickSort is a highly efficient sorting algorithm that follows the divide-and-conquer approach. Below is a step-by-step description of how QuickSort works:
Step 1: Choose a Pivot Element
- Select an element from the array as the pivot. This can be the first, last, middle element, or a random one. The choice of pivot affects the algorithm's performance, especially in already sorted or nearly sorted arrays.
Step 2: Partition the Array
- Rearrange the array elements such that:
- All elements less than the pivot are moved to its left.
- All elements greater than the pivot are moved to its right.
- The pivot is placed in its final sorted position.
- This process is called partitioning.
Step 3: Recursively Apply QuickSort
- Recursively apply the QuickSort algorithm to the left subarray (elements less than the pivot).
- Recursively apply the QuickSort algorithm to the right subarray (elements greater than the pivot).
Step 4: Combine the Results
- Since the subarrays are sorted independently and the pivot is in its correct position, the entire array becomes sorted once the recursive calls complete.
Detailed Partitioning Process
Here’s how partitioning works in detail:
- Select the Pivot: Choose the pivot element (e.g., the last element in the array).
- Initialize Pointers:
- Set a pointer
i to the start of the array (excluding the pivot).
- Set a pointer
j to the start of the array.
- Traverse the Array:
- Move
j from left to right:
- If
array[j] <= pivot, swap array[i] and array[j], then increment i.
- Continue until
j reaches the end of the array.
- Place the Pivot:
- Swap the pivot element (
array[end]) with array[i]. Now, array[i] is the pivot in its correct position.
- Return the Pivot Index:
- Return
i (the index of the pivot) to use in the recursive steps.
Example
Consider the array: [10, 7, 8, 9, 1, 5].
- First Pass:
- Pivot:
5 (last element).
- Partitioned array:
[1, 5, 8, 9, 10, 7].
- Pivot
5 is in its correct position.
- Recursive Calls:
- Left subarray:
[1] (already sorted).
- Right subarray:
[7, 8, 9, 10].
- Second Pass (Right Subarray):
- Pivot:
10 (last element).
- Partitioned array:
[7, 8, 9, 10].
- Pivot
10 is in its correct position.
- Recursive Calls:
- Left subarray:
[7, 8, 9] (repeat the process until all subarrays are sorted).
Pseudocode
def quickSort(arr, low, high):
if low < high:
# Partition the array and get the pivot index
pi = partition(arr, low, high)
# Recursively sort the left and right subarrays
quickSort(arr, low, pi - 1) # Left subarray
quickSort(arr, pi + 1, high) # Right subarray
def partition(arr, low, high):
pivot = arr[high] # Choose the last element as the pivot
i = low - 1 # Index of the smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in its correct position
return i + 1
Time Complexity
- Best/Average Case:
O(n log n) – When the pivot divides the array into roughly equal halves.
- Worst Case:
O(n^2) – When the pivot is the smallest or largest element (e.g., in an already sorted array). This can be mitigated by choosing a random pivot or using the "median-of-three" method.
Space Complexity
O(log n) due to recursive call stack.
QuickSort is widely used due to its efficiency and in-place sorting capability.