// 进行一轮快排并返回当前的中间数 int getMiddle( int* arr, int low, int high ) { auto swaparr = [&]( int i, int j ) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }; if( high - low > 0 ) { int k = arr[low], i = low, j = high; while( i != j ) { //R->L for( ; j > i && arr[j] >= k; j-- ); swaparr( i, j ); //L->R for( ; i < j && arr[i] <= k; i++ ); swaparr( i, j ); } return i; } return -1; } // void _fastSort( int* arr, int low, int high ) { int mid = getMiddle( arr, low, high ); if( mid != -1 ) { _fastSort( arr, low, mid-1 ); _fastSort( arr, mid+1, high ); } }// void fastSort(int* arr,int len) { if( arr && len ) _fastSort( arr, 0, len -1 ); }
时间: 2024-10-27 18:45:41