Fill in the Lomuto partition
Warm-up · Fill in the code · about 6 min
Complete the Lomuto partition. It uses the last value as the pivot, moves smaller values to the front and returns the pivot's final index.
Fill the three blanks.
Example 1
Input:
[[8,3,9,1,6,2,7,5]]
Output:
3
3, 1 and 2 are smaller than 5.
Example 2
Input:
[[4,7,5]]
Output:
1
Only 4 is smaller than 5.
Constraints
- 1 ≤ nums.length ≤ 1,000
- -1,000 ≤ nums[i] ≤ 1,000
Learn the idea first: Quick sort: partitioning and pivots, a visual lesson.
More Quick sort: partitioning and pivots problems
- Partition by hand Warm-up
- Implement quick sort with Lomuto Standard
- Implement quick sort with Hoare Standard
- Spot the bug: the scan stops early Standard
- Sort colors (Dutch national flag) Challenge