768. Quick Sort

MediumRecursionRecursion

Given an integer array nums, sort it in non-decreasing order using the Quick Sort algorithm, implemented recursively. Pick a pivot, partition the array into elements less than, equal to, or greater than the pivot, and recursively sort the less-than and greater-than partitions. Return the sorted array.

Input: An integer array nums.

Output: Return the sorted array as [v1,v2,...].

Examples

Example 1
Input: [5,2,3,1,4]
Output: [1,2,3,4,5]
Explanation: Classic quick sort output.
Example 2
Input: [3,3,3]
Output: [3,3,3]
Explanation: All equal; order preserved.
Example 3
Input: [-1,0,1]
Output: [-1,0,1]
Explanation: Already sorted, including negatives.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →