Skip to content

Quick Sort Algorithm
Step by Step

Partition around a pivot so smaller numbers sit left and larger ones sit right, which puts the pivot in its final place. Then sort both sides.

Step through every swap at your own pace, then share a link to the step where you got stuck.

What You Watch in Every Partition

Pivots, swaps and splits change at every step, and the visualiser tracks them for you.

Partitioning looks short on paper and gets confusing on step nine, when the pivot, the left region and the unsorted rest all change at once.

CodeStory's visualiser shows every comparison and swap, pass by pass. Beside the array, the executing code line stays highlighted. Step through at your pace or let it play.

Try an empty array, a single number and a list full of duplicates. Already sorted input matters most, because a last element pivot splits it badly.

How Quick Sort Partitions the Array

Steps below match the picture, so you can follow them beside the array.

  1. 1Take the last number in the range as the pivot.
  2. 2Scan the range and swap every number at or below the pivot into the left region.
  3. 3Swap the pivot into the slot after the left region, which is its final place.
  4. 4Sort the left side and the right side the same way.
  5. 5Stop when a side holds one number or none.

Where Quick Sort Goes Wrong

Both cases below are switches in the visualiser. Flip one on, replay the same input and watch the state go wrong.

  • Middle pivotSwaps the middle number to the end and uses it as the pivot. On the Already sorted preset the split is even, so the n(n−1)/2 worst case disappears.
  • Bug: pivot into a[i]Swaps the pivot into a[i], the last slot of the ≤ region, instead of a[i + 1]. Smaller numbers land right of the pivot, so most inputs recurse forever and the Already sorted preset comes out in the wrong order.

Quick Sort Cost in Time and Space

Average and worst case decide interview answers, so the visualiser counts real operations.

Time
O(n log n) avg, O(n²) worstcounted from the actual swaps and comparisons on your input
Space
O(log n)extra memory for the recursion stack
Pattern
SortingOrder grows from partitions, one pivot at a time.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Quick Sort Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What does the quick sort visualiser show?

CodeStory's visualiser shows every comparison and swap, pass by pass, on an array you choose. It highlights the line of code being executed, and you can step forward, step back or let it play.

What is the time complexity of quick sort?

Quick sort takes O(n log n) time on average and O(n²) in the worst case, with O(log n) space. CodeStory's visualiser counts real operations as it plays, so you watch the number grow instead of trusting the formula.

Why does my quick sort code fail on some inputs?

Quick sort code fails on some inputs when the pivot lands in the wrong slot or the pivot choice splits unevenly. CodeStory's visualiser ships both cases as switches, so you flip one on, replay the input and watch exactly where the state goes wrong.

What does the middle pivot switch do?

Middle pivot swaps the middle number to the end and uses it as the pivot, so sorted input splits evenly and the n(n−1)/2 worst case disappears.

Can I use my own input?

Yes. Type your own array and the visualiser plays quick sort on it, edge cases included.

Is the visualiser free?

Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.

Stuck on Sorting? Get a Free Callback

Tell CodeStory where sorting loses you, and a mentor plans your next steps.

  1. 1CodeStory finds your starting point, from your first array to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.