Data Structures & Algorithms Sorting Algorithms
💡
Exercise 58

Quick Sort 25 XP Medium

Ctrl+Enter Run Ctrl+S Save

Quick Sort picks a pivot, partitions the array so all smaller elements go left and larger go right, then recursively sorts both sides.

# Pivot = 4: [3, 6, 8, 2, 4, 1] # Partition: [3, 2, 1] [4] [6, 8] # Recursively sort left and right # Result: [1, 2, 3, 4, 6, 8]

Time: O(n log n) average, O(n²) worst (rare with good pivot). Space: O(log n) for recursion stack. In practice, it's often the fastest sorting algorithm.

Pivot strategies: last element, first element, random, or median-of-three.

📋 Instructions
Implement a simple `quick_sort(arr)` using the last element as pivot. Sort `[10, 7, 8, 9, 1, 5]` and print the result.
Choose last element as pivot. Partition: use pointer i starting before the subarray. For each element < pivot, increment i and swap. Finally swap pivot to position i+1. Recursively sort left and right of pivot.
🧪 Test Cases
Input
quick_sort([10, 7, 8, 9, 1, 5])
Expected
[1, 5, 7, 8, 9, 10]
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.