归并排序
1 #include <iostream.h> 2 3 int* mergeSort(int data1[], int data2[], int size1, int size2) 4 { 5 int* temp = new int[size1+size2]; 6 int i = 0,j = 0, t=0; 7 do 8 { 9 if(data1[i]>data2[j]) 10 { 11 temp[t]=data2[j]; 12 t++; 13 j++; 14 } 15 else 16 { 17 temp[t]=data1[i]; 18 t++; 19 i++; 20 } 21 } 22 while(i<size1 && j< size2); 23 if(i>=size1) 24 for(int k=j;k<size2;k++) 25 { 26 temp[t]=data2[k]; 27 t++; 28 } 29 else 30 for(int h=i;h<size1;h++) 31 { 32 temp[t] = data1[h]; 33 t++; 34 } 35 return temp; 36 } 37 38 int main(int argc, char* argv[]) 39 { 40 int data1[] = {12,23,34,56,64,68,99,999,2323}; 41 int data2[] = {2,3,5,65,77,88,100,101,999}; 42 int size1 = sizeof(data1)/sizeof(data1[0]); 43 int size2 = sizeof(data2)/sizeof(data2[0]); 44 45 int* temp = mergeSort(data1,data2,size1,size2); 46 47 for(int k=0;k<size1+size2;k++) 48 cout << temp[k] << " "; 49 cout << endl; 50 return 0; 51 }