FlowLab Logo

Quick Sort Visualizer

SlowFast
64
[0]
34
[1]
25
[2]
12
[3]
22
[4]
11
[5]
90
[6]
Partitioning
Pivot
Comparing
Swapping
Sorted
Operation Logs
No operations yet
Algorithm Info
Time Complexity: O(n log n) average, O(n²) worst case
Space Complexity: O(log n) - recursive call stack
Stable: No - may change relative order of equal elements
Divide & Conquer: Select pivot, partition around it, recursively sort partitions
Worst case: Already sorted array with poor pivot choice
In-place: Yes - sorts within original array with minimal extra space
Implementation
function quickSort(arr, low = 0, high = arr.length - 1) {
  if (low >= high) return;
  
  // Partition the array and get pivot index
  const pivotIndex = partition(arr, low, high);
  
  // Recursively sort left and right partitions
  quickSort(arr, low, pivotIndex - 1);
  quickSort(arr, pivotIndex + 1, high);
}

function partition(arr, low, high) {
  // Choose last element as pivot
  const pivot = arr[high];
  let i = low - 1; // Index of smaller element
  
  for (let j = low; j < high; j++) {
    // If current element is smaller than or equal to pivot
    if (arr[j] <= pivot) {
      i++;
      [arr[i], arr[j]] = [arr[j], arr[i]]; // swap
    }
  }
  
  // Place pivot in correct position
  [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
  
  return i + 1; // Return pivot index
}