题解 | #排序#
排序
http://www.nowcoder.com/practice/2baf799ea0594abd974d37139de27896
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 将给定数组排序 * @param arr int整型vector 待排序的数组 * @return int整型vector */ void swap(int &a ,int &b) { int tmp ; tmp = a; a = b; b = tmp ; } void quickSort(vector &arr, int low , int high) { if(low < high) { int pivot = Partition(arr, low, high); quickSort(arr, low, pivot - 1); quickSort(arr, pivot + 1, high) ; } } int Partition(vector &arr , int low , int high) { srand(time(0)); int index = rand()%(high - low) + low; if(index != low) swap(arr[low] , arr[index]); int pivot = arr[low]; while(low < high) { while(low < high && pivot <= arr[high]) high--; arr[low] = arr[high]; while(low < high && pivot >= arr[low]) low++; arr[high] = arr[low] ; } arr[low] = pivot ; return low; } vector MySort(vector& arr) { // write code here quickSort(arr , 0 , arr.size() - 1 ) ; return arr; } };