Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSASortingQuick SortAlgorithms

Quick Sort — Algorithm Visualized

Learn quick sort — Lomuto partition, pivot placement, O(n log n) average, in-place sorting with interactive partition visualizer.

Jul 4, 20264 min read

Introduction

Quick sort picks a pivot, partitions the array so elements ≤ pivot are left and greater are right, then recursively sorts both sides. Average O(n log n), in-place, cache-friendly — the workhorse of many standard libraries.

Quick index

#Section
1How it works
2Interactive visualizer
3Full implementation
4Complexity analysis
5Real-life examples

1. How it works

plaintext
Pick pivot → partition → pivot in final spot
Recurse on left [lo..p-1] and right [p+1..hi]
VariantPivot choice
LomutoLast element (this visualizer)
HoareTwo pointers from both ends
RandomReduces worst-case on sorted input

Back to index


2. Interactive visualizer

Quick Sort — Step Visualizer

Partition around a pivot — elements ≤ pivot left, greater right — then recurse.

Unsorted

5
0
3
1
8
2
1
3
9
4
2
5
7
6

n = 7 elements

Pick pivot (Lomuto)

pivot = arr[hi]
partition: ≤ pivot left, > pivot right
recurse on both sides

In-place partition around last element as pivot.

Time

O(n log n)*

Space

O(log n)

Stable

No

Tip

Pink = pivot. Green = pivot settled. After partition, the pivot never moves again.

Back to index


3. Full implementation

js
function quickSort(arr, lo = 0, hi = arr.length - 1) {
  if (lo >= hi) return arr
  const p = partition(arr, lo, hi)
  quickSort(arr, lo, p - 1)
  quickSort(arr, p + 1, hi)
  return arr
}
 
function partition(arr, lo, hi) {
  const pivot = arr[hi]
  let i = lo - 1
  for (let j = lo; j < hi; j++) {
    if (arr[j] <= pivot) {
      i++
      ;[arr[i], arr[j]] = [arr[j], arr[i]]
    }
  }
  ;[arr[i + 1], arr[hi]] = [arr[hi], arr[i + 1]]
  return i + 1
}

Function calls with output

js
// ── Demo calls — output shown on the right ──
const arr = [5, 3, 8, 1, 9, 2, 7]
 
quickSort(arr) // → [1, 2, 3, 5, 7, 8, 9]
quickSort([1, 2, 3, 4, 5]) // → [1, 2, 3, 4, 5]  (worst case with bad pivot)

Back to index


4. Complexity analysis

CaseTime
AverageO(n log n)
WorstO(n²) — bad pivot every time
SpaceO(log n) stack depth
StableNo

Warning

Sorted input + last-element pivot → O(n²). Use random pivot or median-of-three in production.

Interview Answer

"Quick sort vs merge sort?"

Quick sort: in-place, faster constants, O(n log n) average but O(n²) worst. Merge sort: O(n log n) guaranteed, stable, needs O(n) extra space.

Back to index


5. Real-life examples

ScenarioWhy quick sort
JavaScript Array.sort()V8 uses Timsort (hybrid), but C++ std::sort is often quicksort intro
Database query executionSorting result sets in memory — cache-friendly in-place partition
Median / percentile dashboardsQuickselect (quick sort variant) finds kth element in O(n) avg
File system directory listingSort files by name/size in memory before rendering
Competitive programming defaultsFast average case, in-place — default choice for general arrays

Transaction report sort

Sort 10k transactions by amount — quicksort partitions in-place around a pivot.

450
120
890
75
320
560

Index → value

Daily transaction amounts — unsorted.

Real-world implementation

function quickSort(arr, lo = 0, hi = arr.length - 1) {
  if (lo >= hi) return arr
  const pivot = arr[hi]
  let i = lo
  for (let j = lo; j < hi; j++) {
    if (arr[j] <= pivot) [arr[i], arr[j]] = [arr[j], arr[i++]]
  }
  [arr[i], arr[hi]] = [arr[hi], arr[i]]
  quickSort(arr, lo, i - 1)
  quickSort(arr, i + 1, hi)
  return arr
}

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const txns = [450, 120, 890, 75, 320, 560]

Side output shows return value after each call — step through to trace execution.

Performance

Quick sort's in-place partitioning makes it the default in many standard libraries when average speed and low memory both matter.

Back to index


Summary

Quick sort partitions in-place around a pivot — fast in practice when pivots are chosen well.

Next reads: Merge Sort · Counting Sort

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime