ALGORITHMS_TODAY.COM
◄ BACK_TO_INDEX.exe
[SORTING] MODULE LOADED

QUICK_SORT

Pick a pivot. Partition. Recurse. Fast in practice. Dangerous with bad pivots.

TIME_COMPLEXITY
O(n log n)
SPACE_COMPLEXITY
O(log n)
DIFFICULTY
INTERMEDIATE
STABILITY
UNSTABLE
PROCESS: QUICK_SORT.exe■ COMPLETE
> step[001/000]
██ PIVOT██ COMPARING██ SWAPPING██ SORTED██ UNSORTED
carets i / j #00e5ff (scan + ≤pivot boundary) · [ lo..hi ] range bracket marks the live partition
CLOCK_SPEED
DECRYPTED_SOURCE.js● LIVE
// QUICK_SORT.js — O(n log n) average time complexity
function quickSort(arr, lo = 0, hi = arr.length - 1) {
  // Base case: a range of 0 or 1 element is already sorted
  if (lo >= hi) return arr;

  // Partition around a pivot, then recurse on each side
  const p = partition(arr, lo, hi);
  quickSort(arr, lo, p - 1);
  quickSort(arr, p + 1, hi);

  return arr; // DECODED
}

// Lomuto partition: uses the last element as the pivot.
function partition(arr, lo, hi) {
  const pivot = arr[hi];
  let i = lo - 1; // boundary of the "<= pivot" region

  for (let j = lo; j < hi; j++) {
    // Grow the region when a value belongs left of the pivot
    if (arr[j] <= pivot) {
      i++;
      [arr[i], arr[j]] = [arr[j], arr[i]];
    }
  }

  // Drop the pivot into its final sorted position
  [arr[i + 1], arr[hi]] = [arr[hi], arr[i + 1]];
  return i + 1;
}
DECRYPTED_LOGIC.txt
01Choose a pivot from the current range (here, the last element)
02Partition: move values <= pivot left, larger values right
03Swap the pivot into the boundary — its final sorted position
04Recurse independently on the left and right partitions
05Base case: ranges of zero or one element are already sorted