What I Learned Today: Quick Sort

kasumbi philkasumbi phil
2 min read

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)

  1. Pick a Pivot: Choose one element in the list to act as the "pivot". I picked the last item.

  2. 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.

  3. Sort the Sides: Use Quick Sort again on the left and right lists (this is the recursive part).

  4. 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! 💪

0
Subscribe to my newsletter

Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

kasumbi phil
kasumbi phil