标签:
最近要去找实习,所以找些东西写写,写了个没优化过的快速排序,接下来贴代码。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 using namespace std; 5 6 void swap(int &num1, int &num2) 7 { 8 int temp = num1; 9 num1 = num2; 10 num2 = temp; 11 } 12 13 14 int partition(int *arry, int p, int r) 15 { 16 int number1 = arry[p]; //以哪个为标准 17 int h = p; //头 18 int t = r + 1; //尾 19 20 while(true) 21 { 22 while(arry[++h] < number1 && t > h); 23 while(arry[--t] > number1); 24 if(h > t) 25 { 26 break; 27 } 28 swap(arry[t],arry[h]); 29 } 30 arry[p] = arry[t]; 31 arry[t] = number1; 32 return t; 33 } 34 35 void quick_sort(int *arry, int p, int r) 36 { 37 if(p < r) 38 { 39 int q = partition(arry, p, r); 40 quick_sort(arry, p, q - 1); 41 quick_sort(arry, q + 1, r); 42 } 43 } 44 45 int main(int argc, const char *argv[]) 46 { 47 int arry[10] = {9,100, 10, 1, 6, 7, 8, 54, 32, 65}; 48 quick_sort(arry, 0, 9); 49 for(auto &item: arry){ 50 cout << item << " "; 51 } 52 cout << endl; 53 return 0; 54}
标签:
原文地址:http://www.cnblogs.com/bigshowxin/p/4374311.html