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.
- 1Take the last number in the range as the pivot.
- 2Scan the range and swap every number at or below the pivot into the left region.
- 3Swap the pivot into the slot after the left region, which is its final place.
- 4Sort the left side and the right side the same way.
- 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.
Practise What You Watched
Solve a few sorting problems while the pivot steps stay fresh.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory finds your starting point, from your first array to interview ready.
- 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
- 3You decide. No pressure, no payment on the call.