Quick Sort visualization
Partition around a pivot so smaller values go left and larger go right, then recurse.
Lomuto partition: the last element is the pivot. Index i marks the end of the "≤ pivot" zone; j scans the rest. Finally the pivot is swapped right after that zone — into its final position.
Average O(n log n) and in-place, but a bad pivot (e.g. sorted input with last-element pivot) degrades to O(n²).
Complexity
Time: best O(n log n), average O(n log n), worst O(n²). Space: O(log n). Difficulty: Intermediate.
Pseudocode
procedure quickSort(A, lo, hi):
if lo ≥ hi: return
pivot ← A[hi]; i ← lo − 1
for j ← lo to hi−1:
if A[j] ≤ pivot:
i ← i + 1; swap(A[i], A[j])
swap(A[i+1], A[hi]) // pivot in place
quickSort(A, lo, i); quickSort(A, i+2, hi)More sorting algorithms
- Bubble Sort — Repeatedly swap adjacent out-of-order pairs; the largest value bubbles to the end each pass.
- Selection Sort — Find the minimum of the unsorted part and swap it to the front.
- Insertion Sort — Grow a sorted prefix by inserting each new value into its correct slot.
- Cocktail Shaker Sort — Bubble sort that alternates direction, moving large values right and small values left.
- Shell Sort — Insertion sort over shrinking gaps, so values travel long distances early.
- Merge Sort — Divide in half, sort each half recursively, then merge the two sorted halves.
- Heap Sort — Build a max-heap, then repeatedly move the root (maximum) to the end and re-heapify.
- Counting Sort — Count occurrences of each value, then rebuild the array from the counts.
- Radix Sort (LSD) — Sort by the ones digit, then tens, then hundreds — each pass is a stable bucket sort.