What I Learned Today: Quick Sort

Today, I learned about Quick Sort, one of the most powerful and efficient sorting algorithms. It works using a strategy called “divide and conquer” — which simply means it breaks the list into smaller pieces, sorts those, and puts everything back together.
🚀 How It Works (My Understanding)
Pick a Pivot: Choose one element in the list to act as the "pivot". I picked the last item.
Divide the List:
Everything less than or equal to the pivot goes into the
left
list.Everything greater than the pivot goes into the
right
list.
Sort the Sides: Use Quick Sort again on the
left
andright
lists (this is the recursive part).Put It Together: Combine everything like this:
sorted_left + [pivot] + sorted_right
🧠 My Quick Sort Code
def quick_sort(arr):
n = len(arr)
if n <= 1:
return arr
pivot = arr[-1]
left = []
right = []
for item in arr[:-1]:
if item <= pivot:
left.append(item)
else:
right.append(item)
left = quick_sort(left)
right = quick_sort(right)
return left + [pivot] + right
arr = [12, 30, 50, 47, 70, 59]
print(quick_sort(arr))
✅ Output:
[12, 30, 47, 50, 59, 70]
💡 Final Thoughts
Quick Sort is fast, especially for big lists. But I also learned that it’s very important to call the recursive steps only after dividing the list completely — not during the loop!
I’m excited I finally understand how this works. On to the next one! 💪
Subscribe to my newsletter
Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
