Skip to the code
cf.
+25 XP

#198 Quick Sort Pivot Order

medium

A sorting visualizer highlights each pivot as quick sort picks it. The sort uses the Lomuto partition with the last item of each part as the pivot (smaller items move left in order, as in quickSort from the algorithm page), then sorts the left part, then the right part. A part with fewer than 2 items is not partitioned and has no pivot.

Write pivotOrder(nums) that returns the pivot values in the order they are chosen.

Example 1

Input: nums = [7,2,9,4,3,8,5]
Output: [5,3,7,9]

5 splits into [2, 4, 3] and [9, 8, 7]. The left part picks 3 and leaves single items. The right part picks 7, which leaves [8, 9], and that part picks 9.

Example 2

Input: nums = [1,2,3,4]
Output: [4,3,2]

On sorted input every pivot is the largest, leaving one big part each time.

Constraints

  • 0 <= nums.length <= 500
  • -1000 <= nums[i] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).