How quicksort works
Let us see how How quicksort works
🧩 Imagine this:
You have a bunch of toys scattered around — cars, dolls, and action figures — all of different sizes. You want to line them up from smallest to biggest.
⚙️ Step 1: Pick a “pivot”
You choose one toy — let’s say a medium-sized car — and call it the pivot. This toy will help you decide where everything else goes.
⚖️ Step 2: Compare other toys
You look at each toy:
- If it’s smaller than the pivot → put it on the left side.
- If it’s bigger than the pivot → put it on the right side.
Now the pivot is in its correct spot — everything smaller is on the left, everything bigger is on the right!
🔁 Step 3: Do the same again
Now you do the same sorting for the left group and the right group — each time choosing a new pivot. Keep splitting and sorting until every group has just one toy.
🏁 Step 4: Done!
When you put all the groups back together (left + pivot + right), everything is perfectly sorted.
🌟 The pivot’s role
The pivot is like the team leader who decides:
“You smaller guys go left, bigger ones go right!”
Without the pivot, you wouldn’t know how to divide the toys into groups to sort them faster.
🧠 Fun fact:
Quicksort is called “quick” because it avoids comparing every toy with every other toy — it keeps dividing and conquering!