c++十大排序——快速排序
1
#include<iostream> using namespace std; void quickSort(int arr[], int begin, int end) { if (begin >= end) return; int left = begin; int right = end; int temp = arr[left]; while (left < right) { //从后往前找比他小的放前面,从前往后找比它大的放后面 //以第一个数为基准,必须先从后往前走,再从前往后走 while (left < right && arr[right] >= temp) { right--; } //跳出此循环,代表right找到了比temp小的数字,所以此时arr[left]=arr[right] if (left < right) { arr[left] = arr[right]; } while (left < right && arr[left] <= temp) { left++; }//同理 if (left < right) { arr[right] = arr[left]; } if (left == right) { arr[left] = temp; } } quickSort(arr, begin, left - 1); quickSort(arr, left + 1, end); } int main() { int arr[11] = { 5,6,3,2,7,8,9,1,4,0,0 }; quickSort(arr, 0, 10); for (auto x : arr) { cout << x << " "; } return 0; }