101 Logo
onenoughtone

Quick Sort Pattern

What is the Quick Sort Pattern?

The Quick Sort pattern is a divide and conquer approach that partitions the data around a pivot element, then recursively sorts the partitions.

It's characterized by its efficient partitioning step, which divides the data into elements less than the pivot and elements greater than the pivot.

Key Steps

  1. Choose a Pivot: Select an element from the array as the pivot
  2. Partition: Rearrange the array so that elements less than the pivot come before it, and elements greater than the pivot come after it
  3. Recursively Sort: Apply the same process to the subarrays on the left and right of the pivot

Quick Sort Implementation

Here's an implementation of the classic quick sort algorithm:

function quickSort(arr, low = 0, high = arr.length - 1) { if (low < high) { // Partition the array and get the pivot index const pivotIndex = partition(arr, low, high); // Recursively sort the subarrays quickSort(arr, low, pivotIndex - 1); // Sort left subarray quickSort(arr, pivotIndex + 1, high); // Sort right subarray } return arr; } function partition(arr, low, high) { // Choose the rightmost element as the pivot const pivot = arr[high]; // Index of the smaller element let i = low - 1; // Traverse through all elements // compare each element with the pivot for (let j = low; j < high; j++) { // If current element is smaller than or equal to the pivot if (arr[j] <= pivot) { // Increment index of smaller element i++; // Swap elements [arr[i], arr[j]] = [arr[j], arr[i]]; } } // Swap the pivot element with the element at (i + 1) // This puts the pivot in its correct sorted position [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]]; // Return the pivot's index return i + 1; }

Time and Space Complexity

Time Complexity:

Average Case: O(n log n)
Worst Case: O(n²) when the array is already sorted or nearly sorted
Best Case: O(n log n)

Space Complexity:

O(log n) for the recursion stack in the average case, O(n) in the worst case.

Application: Quick Select

The quick sort pattern can be adapted to find the kth smallest element in an array in O(n) average time:

function quickSelect(arr, k) { // Find the kth smallest element (0-indexed) return quickSelectHelper(arr, 0, arr.length - 1, k); } function quickSelectHelper(arr, low, high, k) { // If the array contains only one element, return that element if (low === high) { return arr[low]; } // Partition the array and get the pivot index const pivotIndex = partition(arr, low, high); // If the pivot is the kth element, return it if (pivotIndex === k) { return arr[pivotIndex]; } // If k is less than the pivot index, search in the left subarray if (k < pivotIndex) { return quickSelectHelper(arr, low, pivotIndex - 1, k); } // If k is greater than the pivot index, search in the right subarray return quickSelectHelper(arr, pivotIndex + 1, high, k); } function partition(arr, low, high) { // Same partition function as in quickSort const pivot = arr[high]; let i = low - 1; for (let j = low; j < high; j++) { if (arr[j] <= pivot) { i++; [arr[i], arr[j]] = [arr[j], arr[i]]; } } [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]]; return i + 1; }
IntroVisualizePatternsPractice
101 Logo
onenoughtone