Quicksort: partition
Quicksort splits the array around a pivot. Smaller values end up left of it, larger ones right.
Rules
- Pivot: last element
- i marks the end of the small part
- Finally swap the pivot to position i
Your turn
Three problems on this topic. Separate lists with commas.
- 1 · Quicksort
- 2
- 3
Quicksort partitions with Lomuto’s scheme using the last element as pivot. What does the array look like afterwards?
[46, 14, 35, 15, 58, 47]
Array (comma-separated)
Final position of the pivot (index from 0)
Separate lists with commas, such as 3, 5, 7. Spaces don’t matter.
Worked example
Quicksort partitions with Lomuto’s scheme using the last element as pivot. What does the array look like afterwards?
[38, 10, 44, 12, 49, 9, 52, 56, 5]
Worked solution
- 1Pivot: 5
- 2j runs from the left, each element ≤ pivot is swapped to the front: [5, 10, 44, 12, 49, 9, 52, 56, 38]
- 3The pivot ends at index 0.
Typical mistake: After partitioning only the pivot is guaranteed to be in its final place.
Common questions
When does quicksort get slow?
When the pivot is always the smallest or largest element, such as on sorted data.
