题解 | #排序#

排序

https://www.nowcoder.com/practice/2baf799ea0594abd974d37139de27896

#include <cmath>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 将给定数组排序
     * @param arr int整型vector 待排序的数组
     * @return int整型vector
     */
    vector<int> MySort(vector<int>& arr) {
        // write code here
        //冒泡排序
        QuickSort(arr,0,arr.size()-1);
        return arr;
    }
    void QuickSort(vector<int>&arr,int low,int high){
        if (low<high) {
            int mid = Partition(arr,low,high);
            QuickSort(arr,  low, mid-1);
            QuickSort(arr, mid+1, high); 
        }
    }
    int Partition(vector<int>&arr,int low, int high){
        int left = low;
        int right = high;
        int temp = arr[left];
        while(left<right){
            while (arr[right]>=temp && left<right) {
                right--;
            }
            arr[left] = arr[right];
            while (arr[left]<temp&&left<right) {
                left++;
            }
           
            arr[right]=arr[left];
        }
        arr[left] = temp;
        return left;
    }

};

全部评论

相关推荐

07-21 12:41
已编辑
门头沟学院 Java
steelhead:不是你的问题,这是社会的问题。
点赞 评论 收藏
分享
07-29 14:27
门头沟学院 Java
LMQICU:大暴雨阁下真的没绷住
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务