LeetCode912 排序数组


题目

给你一个整数数组 nums,请你将该数组升序排列。

 示例 1: 
输入:nums = [5,2,3,1]
输出:[1,2,3,5]

 示例 2: 
输入:nums = [5,1,1,2,0,0]
输出:[0,0,1,1,2,5]

 提示: 
 1 <= nums.length <= 5 * 10? 
 -5 * 10? <= nums[i] <= 5 * 10? 

方法

随机数快排

  • 时间复杂度:O(nlogn),n为数组的长度
  • 空间复杂度:O(h),递归深度
class Solution {
    public int[] sortArray(int[] nums) {
        randomizedQuickSort(nums,0,nums.length-1);
        return nums;
    }
    private void randomizedQuickSort(int[] nums,int l,int r){
        if(l