Skip to content

Visualizers

Quick Sort Visualizer

Step-through animation of Quick Sort showing partitioning and pivot choices.

native (Canvas API) Client-side
Ready to visualize

Execution Log

Comparisons: 0
Swaps: 0
Current Action:

Idle

Legend:

Unsorted
Pointers (i, j)
Pivot
Sorted / Pivot Placed

Quick Sort Algorithm

Quick Sort is a highly efficient, in-place sorting algorithm. It uses a divide-and-conquer strategy to partition a list into smaller sublists, which are then sorted recursively.

How It Works

  1. Pick an element from the array to serve as the pivot (e.g. the last element).
  2. Reorder the array so that all elements smaller than the pivot come before it, and all elements larger than the pivot come after it. This step is called partitioning.
  3. Recursively apply the above steps to the sub-arrays on the left and right of the pivot.

Implementation

function quickSort(arr, l = 0, r = arr.length - 1) {
  if (l < r) {
    const pIdx = partition(arr, l, r);
    quickSort(arr, l, pIdx - 1);
    quickSort(arr, pIdx + 1, r);
  }
  return arr;
}

function partition(arr, l, r) {
  const pivot = arr[r];
  let i = l - 1;
  for (let j = l; j < r; j++) {
    if (arr[j] < pivot) {
      i++;
      const temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
    }
  }
  const temp = arr[i + 1];
  arr[i + 1] = arr[r];
  arr[r] = temp;
  return i + 1;
}