给你一个整数数组 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 104
    -5
    104 <= nums[i] <= 5 * 104


    快速排序

    1. class Solution {
    2. int[] nums;
    3. public int[] sortArray(int[] nums) {
    4. int n = nums.length;
    5. this.nums = nums;
    6. quick_sort(nums,0,n-1);
    7. return nums;
    8. }
    9. public void quick_sort(int[] nums, int l, int r){
    10. if(l >= r) return;
    11. int x = nums[l+r >> 1];
    12. int i = l-1, j = r+1;
    13. while(i < j){
    14. do i++; while(nums[i] < x);
    15. do j--; while(nums[j] > x);
    16. if(i < j){
    17. int tem = nums[i];
    18. nums[i] = nums[j];
    19. nums[j] = tem;
    20. }
    21. }
    22. quick_sort(nums,l,j);
    23. quick_sort(nums,j+1,r);
    24. }
    25. }

    归并排序

    1. class Solution {
    2. int[] nums;
    3. int[] tem;
    4. public int[] sortArray(int[] nums) {
    5. int n = nums.length;
    6. this.nums = nums;
    7. tem = new int[n];
    8. merge_sort(nums,0,n-1);
    9. return nums;
    10. }
    11. public void merge_sort(int[] nums, int l, int r){
    12. if(l >= r) return;
    13. int mid = l+r >> 1;
    14. merge_sort(nums,l,mid);
    15. merge_sort(nums,mid+1,r);
    16. int i = l, j = mid+1, k = 0;
    17. while(i <= mid && j <= r)
    18. if(nums[i] <= nums[j]) tem[k++] = nums[i++];
    19. else tem[k++] = nums[j++];
    20. while(i <= mid) tem[k++] = nums[i++];
    21. while(j <= r) tem[k++] = nums[j++];
    22. for(i = l, j = 0; i <= r; ++i,++j) nums[i] = tem[j];
    23. }
    24. }