Implement quick sort with Lomuto
Standard · Write code · about 15 min
Sort nums in ascending order with quick sort. Use the Lomuto partition: the last value is the pivot, and a boundary index i collects the smaller values at the front.
Sort in place and return the same array.
Example 1
Input:
[[5,2,8,1,9,4]]
Output:
[1,2,4,5,8,9]
Example 2
Input:
[[3,1,2]]
Output:
[1,2,3]
Example 3
Input:
[[]]
Output:
[]
An empty array is already sorted.
Constraints
- 0 ≤ nums.length ≤ 1,000
- -1,000 ≤ nums[i] ≤ 1,000
- Sort in place and return the same array
Learn the idea first: Quick sort: partitioning and pivots, a visual lesson.
More Quick sort: partitioning and pivots problems
- Partition by hand Warm-up
- Fill in the Lomuto partition Warm-up
- Implement quick sort with Hoare Standard
- Spot the bug: the scan stops early Standard
- Sort colors (Dutch national flag) Challenge