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.
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.
Input: nums = [1,2,3,4] Output: [4,3,2]
On sorted input every pivot is the largest, leaving one big part each time.
0 <= nums.length <= 500-1000 <= nums[i] <= 1000Run 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).
I'm here to help with #198 Quick Sort Pivot Order. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.