Quick Sort:
Quick Sort:
What is Quick Sort?
Quick Sort is a way to arrange numbers in order by picking one number as a "pivot" and then putting smaller numbers on one side and bigger numbers on the other. It repeats this process for each side until everything is sorted.
How does it work?
- Pick a pivot (any number from the list, often the last one).
- Compare every other number to the pivot:
- Numbers smaller than the pivot go to the left.
- Numbers bigger than the pivot go to the right.
- Now the pivot is in the correct position.
- Repeat the process for the left and right sides until the whole list is sorted.
Step-by-step process:
- Pick a pivot.
- Divide the list into two groups: smaller numbers and bigger numbers.
- Sort the smaller and bigger groups by doing the same steps again.
- Combine everything to get the sorted list.
Example:
List: [7, 3, 8, 4]
- Pick a pivot (let’s choose 4).
- Compare numbers to 4:
- Smaller: [3]
- Pivot: [4]
- Bigger: [7, 8]
- Now sort the smaller group [3] (already sorted) and the bigger group [7, 8] (already sorted).
- Combine: [3, 4, 7, 8].
Key idea:
Use a pivot to split the list into smaller and bigger parts, then repeat the process for each part.
Comments
Post a Comment