标签:
1 #include <iostream> 2 #include <iterator> 3 #include <vector> 4 using namespace std; 5 6 void displayArray(int* const,const int&); 7 void displayVec(vector<int> &); 8 9 void MERGE(int * const A,const int &p,const int &q,const int &r){ 10 vector<int> L; 11 for (int i = p; i <= q; ++i){ 12 L.push_back(A[i]); 13 } 14 vector<int> R; 15 for (int i = q + 1; i <= r; ++i){ 16 R.push_back(A[i]); 17 } 18 19 auto i = L.begin(); 20 auto j = R.begin(); 21 for (int k = p; k <= r; ++k){ 22 if (i != L.end() && j != R.end()){ 23 if (*i <= *j){ 24 A[k] = *i; 25 ++i; 26 } 27 else{ 28 A[k] = *j; 29 ++j; 30 } 31 } 32 else{ 33 if (i == L.end()){ 34 A[k] = *j; 35 ++j; 36 } 37 else{ 38 A[k] = *i; 39 ++i; 40 } 41 } 42 } 43 } 44 45 void MERGE_SORT(int *const A,const int &p,const int &r){ 46 if (p < r){ 47 int q = (p + r) / 2; 48 MERGE_SORT(A, p, q); 49 MERGE_SORT(A, q + 1, r); 50 MERGE(A, p, q, r); 51 } 52 } 53 54 void displayArray(int * const a,const int &length){ 55 for (int i = 0; i < length; ++i){ 56 cout << " " << a[i]; 57 } 58 cout << endl; 59 } 60 61 int main(void) 62 { 63 int a[] = {2,6,21,7,3,2,76,23,8,31,45,1,42,1,57,2,241,3}; 64 MERGE_SORT(a, 0, end(a)-begin(a)-1); 65 displayArray(a, end(a)-begin(a)); 66 system("pause"); 67 return 0; 68 }
标签:
原文地址:http://www.cnblogs.com/lhyz/p/4305411.html