912. 排序数组

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

 

 

class Solution {
    public int[] sortArray(int[] nums) {
        if (nums.length <= 1)
        {
            return nums;
        }
        // qSort(nums,0,nums.length-1);
        // selectSort(nums);
        // insertSort(nums);
        mergeSort(nums,0,nums.length-1);
        return nums;
    }
    //快速排序
    void qSort(int[] arr, int s, int e)
    {
        int l = s, r = e;
        if (l < r)
        {
            int temp = arr[l];
            while(l < r)
            {
                while (l < r && arr[r] >= temp)
                {
                    r--;
                }
                if (l < r)
                {
                    arr[l] = arr[r];
                }
                while (l < r && arr[l] < temp)
                {
                    l++;
                }
                if (l < r)
                {
                    arr[r] = arr[l];
                }
            }
            arr[l] = temp;
            qSort(arr,s,l);
            qSort(arr,l + 1, e);
        }
    }
    void selectSort(int[] arr)
    {
        int min;
        for (int i = 0; i < arr.length; i++)
        {
            min = i;
            for (int j = i; j < arr.length; j++)
            {
                if (arr[j] < arr[min])
                {
                    min = j;
                }
            }
            if (min != i)
            {
                int temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
        }
    }
    void insertSort(int arr[])
    {
        for(int i = 1; i < arr.length; i++)
        {
            int rt = arr[i];
            for (int j = i - 1; j >= 0; j --)
            {
                if (rt < arr[j])
                {
                    arr[j+1] = arr[j];
                    arr[j] = rt;
                }else{
                    break;
                }
            }
        }
    }
    void mergeSortInOrder(int[] arr, int bgn, int mid, int end)
    {
        int l = bgn, m = mid + 1, e = end;
        int[] arrs = new int[end - bgn + 1];
        int k = 0;
        while (l <= mid && m <= e)
        {
            if(arr[l] < arr[m])
            {
                arrs[k++] = arr[l++];
            }else{
                arrs[k++] = arr[m++];
            }
        }
        while (l <= mid)
        {
            arrs[k++] = arr[l++];
        }
        while (m <= e)
        {
            arrs[k++] = arrs[m++];
        }
        for (int i = 0; i < arrs.length; i++)
        {
            arr[i + bgn] = arrs[i];
        }
    }
    void mergeSort(int[] arr, int bgn, int end)
    {
        if (bgn >= end)
        {
            return;
        }
        int mid = (bgn + end) >>1;
        mergeSort(arr, bgn,mid);
        mergeSort(arr,mid+1,end);
        mergeSortInOrder(arr,bgn,mid,end);
    }
}

  

posted on 2021-07-15 13:15  Hebye  阅读(60)  评论(0编辑  收藏  举报

导航