Skip to content

Quick Sort Visualizer

Pick a pivot, move smaller values to its left and bigger ones to its right, then repeat on each side.

Comparisons: 0 Swaps: 0 Values: 0
  • Unsorted
  • Comparing
  • Swapping
  • Pivot
  • Sorted
Step 1 of 1

quickSort(a, lo, hi):  if lo ≥ hi: return  p = partition(a, lo, hi)  quickSort(a, lo, p − 1)  quickSort(a, p + 1, hi) partition(a, lo, hi):   // Lomuto  pivot = a[hi]; i = lo  for j = lo to hi − 1    if a[j] < pivot      swap a[i] and a[j]; i = i + 1  swap a[i] and a[hi]  return i
Time and space complexity
MeasureValue
Best caseO(n log n)
Average caseO(n log n)
Worst caseO(n²)
Extra spaceO(log n) stack
StableNo
In placeYes

Quick sort is the sort most libraries build on because it is fast in practice. It picks a pivot value, then partitions the array so every smaller value ends up left of the pivot and every other value to its right. The pivot is now in its final place, and quick sort repeats the process on the two sides. This visualizer shows the pivot in purple, the boundary index i in its own colour, and greys out everything outside the part being partitioned.

It uses the Lomuto partition with the last element as the pivot. Try a sorted or reversed input to see the worst case: the pivot is always the largest or smallest value, the partitions are lopsided, and the comparison count climbs towards n²/2.

How to use the Quick Sort Visualizer tool

  1. Pick an array size and an input: random, nearly sorted, reversed or few unique values. You can also type your own numbers.
  2. Press Play to watch the sort, or Step to move one comparison or swap at a time. Back goes one step back.
  3. Follow the colours: orange bars are being compared, red bars are being swapped or written, green bars are in their final place.
  4. Read the message under the controls and the highlighted pseudocode line to see exactly what the algorithm is doing.
  5. Change the speed, switch the code tab to C++, Java, Python or JavaScript, and compare the counters with other inputs.

Why use ToolOrca Quick Sort Visualizer

  • Animated bars with play, pause, step and step back
  • Speed slider and a position slider to jump to any step
  • Comparison and swap counters
  • Random, nearly sorted, reversed and few-unique inputs, or your own numbers
  • Highlighted pseudocode line for every step
  • Code in C++, Java, Python and JavaScript
  • Works on phones and tablets, keyboard accessible

How quick sort works

Partition (Lomuto): take pivot = a[hi] and i = lo. For each j from lo to hi − 1, if a[j] is smaller than the pivot, swap a[i] and a[j] and increase i. At the end, swap a[i] with a[hi]: the pivot lands at index i with smaller values on its left and the rest on its right. Then sort lo..i − 1 and i + 1..hi the same way. A range of zero or one element is already sorted.

The Hoare partition, which moves two pointers towards each other, does fewer swaps and is common in real code. Production versions also pick the pivot more carefully: the median of three values or a random element.

Time and space complexity of quick sort

If each pivot splits its range roughly in half, there are about log₂ n levels with O(n) work each: O(n log n), which is also the average over random inputs. If the pivot is always the smallest or largest value, one side is empty every time and the time becomes O(n²); with the last element as pivot, that happens on already sorted input. Quick sort sorts in place and needs O(log n) stack space on average (O(n) in the worst case unless you recurse on the smaller side first). It is not stable.

When to use quick sort

Quick sort is the default choice for sorting arrays in memory: C++ std::sort uses introsort (quick sort that switches to heap sort if the recursion gets too deep, and insertion sort for small pieces), and Java sorts primitive arrays with a dual-pivot quick sort. Avoid a naive version on data that may already be sorted, and use merge sort when you need stability.

Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.

Frequently asked questions

What is the time complexity of quick sort?

O(n log n) on average and in the best case, O(n²) in the worst case when the pivots split the array very unevenly.

Why is quick sort fast if its worst case is O(n²)?

Good pivot choices (random or median of three) make the worst case extremely unlikely, and quick sort’s in-place partitioning has low overhead and uses the CPU cache well.

What is a pivot?

The value the array is partitioned around. After partitioning, the pivot is in its final sorted position, with smaller values on its left and the rest on its right.

Is quick sort stable?

No. Partitioning swaps values over long distances, which can change the order of equal values.

What is the difference between Lomuto and Hoare partition?

Lomuto uses one index scanning left to right and is easier to understand; Hoare uses two indexes moving towards each other and makes about three times fewer swaps on average.