题解 | #排序#
排序
https://www.nowcoder.com/practice/2baf799ea0594abd974d37139de27896
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 将给定数组排序 * @param arr int整型vector 待排序的数组 * @return int整型vector */ //快速排序 int partition(vector<int>& arr,int p,int r){ int temp = arr[r]; int i=p; for(int j=p;j<r;j++){ if(arr[j]<temp){ swap(arr[i],arr[j]); i++; } } swap(arr[i],arr[r]); return i; } void Mysort(vector<int>& arr,int p,int r){ if(p>=r) return ; int q=partition(arr,p,r); Mysort(arr,p,q-1); Mysort(arr,q+1,r); } vector<int> MySort(vector<int>& arr) { // write code here int n = arr.size(); Mysort(arr,0,n-1); return arr; } };