Skip to content
Onsite

Quick Sort

MediumP1Coding

Implement quick sort. Given an integer array nums, return a new array with the same elements in ascending order.

Do not call a built-in sort; partition around a pivot yourself. Your version should handle many equal values without degrading to quadratic time.

Example 1

Input
nums = [5,2,3,1]
Output
[1,2,3,5]

Example 2

Input
nums = [5,1,1,2,0,0]
Output
[0,0,1,1,2,5]

Example 3

Input
nums = [2,2,2]
Output
[2,2,2]

Constraints

  • 0 <= nums.length <= 5 * 10^4
  • -10^5 <= nums[i] <= 10^5

Hints

Run checks the 3 visible tests, Submit all 8.