Sorting Algorithms

How Quicksort Works

  • Waiting
  • Being compared
  • Just moved
  • In final position
1/67

Quick sort, 12 values in random order.

Settings

More options
Starting order

Two to 48 numbers, used in the order you type them.

Quick Sort

  1. 1stack = [(0, n-1)]
  2. 2while stack not empty:
  3. 3 (lo, hi) = stack.pop()
  4. 4 pivot = a[hi]
  5. 5 i = lo
  6. 6 for j in lo..hi-1:
  7. 7 if a[j] <= pivot:
  8. 8 swap(i, j); i += 1
  9. 9 swap(i, hi) // pivot final
  10. 10 push (lo, i-1) and (i+1, hi)

How the Seven Compare

AlgorithmBestAverageWorstExtra memoryStable
BubbleO(n)O(n²)O(n²)O(1)Yes
InsertionO(n)O(n²)O(n²)O(1)Yes
SelectionO(n²)O(n²)O(n²)O(1)No
ShellO(n log n)O(n√n)O(n²)O(1)No
MergeO(n log n)O(n log n)O(n log n)O(n)Yes
QuickO(n log n)O(n log n)O(n²)O(log n)No
HeapO(n log n)O(n log n)O(n log n)O(1)No

Scroll the table sideways for the rest of the columns.

Stable means equal values keep their original order.