七种排序算法源码

1.【插入排序】

[cpp]  view plain copy
  1. void InsertSort(int* p ,int size)  
  2. {  
  3.     for(int i=1;i<size;i++)  
  4.     {  
  5.         int j   =   i;  
  6.         int t   = p[i];  
  7.         for(; j > 0 && p[j-1] > t; j--)  
  8.             p[j] = p[j-1];  
  9.         p[j] = t;  
  10.     }     
  11. }  

2.【选择排序】

[cpp]  view plain copy
  1. void SelectSort(int* arr,int size)  
  2. {  
  3.     for(int i=0;i<size;i++)  
  4.     {  
  5.         int psn=i;  
  6.         for(int j=i;j<size;j++)  
  7.         {  
  8.             if(arr[j]<arr[psn])  
  9.                 psn = j;  
  10.         }     
  11.         swap(arr[psn],arr[i]);  
  12.     }     
  13. }  

3.【冒泡排序】

[cpp]  view plain copy
  1. void BubbleSort(int* arr,int size)  
  2. {  
  3.     bool cg = true;       
  4.     int  n  = 0;  
  5.     do  
  6.     {  
  7.         cg = false;   
  8.         for (int i=0;i<size-1-n;i++)  
  9.         {  
  10.             if (arr[i]>arr[i+1])  
  11.             {  
  12.                 cg = true;  
  13.                 swap(arr[i],arr[i+1]);        
  14.             }  
  15.         }     
  16.         n++;  
  17.     } while (cg);  
  18. }  

4.【快速排序】

[cpp]  view plain copy
  1. void QuickSort(int* arr,int size)  
  2. {  
  3.     int *L,*R;  
  4.     if(size <= 1) return;  
  5.     if(2 == size)  
  6.     {  
  7.        if(arr[0] > arr[1])  
  8.          swap(arr[0],arr[1]);  
  9.        return;  
  10.     }   
  11.     L = &arr[1];  
  12.     R = &arr[size -1];  
  13.     while(L < R)  
  14.    {  
  15.       while(L<R && *L < *arr) ++L;  
  16.       while(R>arr && !(*R < *arr)) --R;  
  17.      if(L<R) swap(*R,*L);  
  18.    }  
  19.    swap(*R,*arr);  
  20.    QuickSort(arr, R-arr);  
  21.    QuickSort(R+1,size -1 -(R-arr));  
  22. }  

5.【堆排序】

[cpp]  view plain copy
  1. void Shift(int* heap, int start, int end)  
  2. {  
  3.        int tmp = heap[start];   
  4.        int parent = start;  
  5.        int child = parent * 2 + 1;   
  6.        while(child <= end)  
  7.      {  
  8.        if(child < end &&   
  9.              heap[child] < heap[child+1])  
  10.                 child++;  
  11.   
  12.          if(tmp < heap[child])  
  13.         {  
  14.              heap[parent] = heap[child];  
  15.              parent = child;  
  16.              child = 2 * parent +1;  
  17.         } else  break;  
  18.      }  
  19.     heap[parent] = tmp;  
  20. }  
  21.   
  22. void BuildHeap(int* heap,int size)  
  23. {  
  24.       for(int i=size/2-1; i >=0; i--)  
  25.          Shift(heap,i,size-1);  
  26.   
  27. }  
  28.   
  29. void HeapSort(int* heap,int size)  
  30. {  
  31.     BuildHeap(heap,size);  
  32.     for(int i = size-1; i>0; i--)  
  33.     {  
  34.           swap(heap[0],heap[i]);  
  35.           Shift(heap,0,i-1);  
  36.     }  
  37. }  

6.【归并排序】

[cpp]  view plain copy
  1. void Merge(int* arr,int first,int mid,int last)  
  2. {  
  3.      int size = last -first + 1;  
  4.      int* tmp = new int[size];  
  5.      int   b1 = first;  
  6.      int   e1 = mid;  
  7.      int   b2 = mid + 1;  
  8.      int   e2 = last;  
  9.      int    k = 0;  
  10.      while(b1<=e1 && b2<=e2)  
  11.        if(arr[b1]<arr[b2])  
  12.           tmp[k++] = arr[b1++];  
  13.        else tmp[k++] = arr[b2++];  
  14.       
  15.      while(b1<=e1)   tmp[k++]=arr[b1++];  
  16.      while(b2<=e2)   tmp[k++]=arr[b2++];  
  17.      for(int i=0; i<size; i++)  
  18.         arr[first + i] = tmp[i];  
  19.      delete tmp;     
  20. }  
  21.   
  22. void MergeSort(int* arr,int first,int last)  
  23. {  
  24.     if(first < last)  
  25.     {  
  26.        int  mid = (first +last) / 2;  
  27.        MergeSort(arr,first,mid);  
  28.        MergeSort(arr,mid+1,last);  
  29.        Merge(arr,first,mid,last);  
  30.     }  
  31. }  


7.【希尔排序】

[cpp]  view plain copy
  1. void ShellSort(int *p, int size)  
  2. {  
  3.    int iTemp;  
  4.    forint iStep = size/2; iStep > 0; iStep = iStep/2 ) {  
  5.      forint i = iStep; i < size; ++i ) {  
  6.     iTemp = p[i];  
  7.     int j = i;  
  8.     for( ; j >= iStep; j -= iStep ) {     
  9.       if( iTemp < p[j-iStep] ) {  
  10.         p[j] = p[j-iStep];  
  11.       } else {  
  12.             break;  
  13.           }  
  14.     }  
  15.     p[j] = iTemp;  
  16.       }  
  17.     }  
  18. }  

【代码测试】

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<string>  
  3. using namespace std;  
  4.   
  5. typedef void(*SortFunc)(int*, intint*, int*);  
  6.   
  7. // 插入排序  
  8. void InsertSort(int* p ,int size, int* move_cnt, int* compare_cnt)    
  9. {    
  10.     for(int i=1;i<size;i++)    
  11.     {    
  12.         int j   =   i;    
  13.         int t   = p[i];   
  14.         ++*move_cnt;   
  15.         ++*compare_cnt;  
  16.         for(; j > 0 && p[j-1] > t; j--) {   
  17.             ++*compare_cnt;  
  18.             p[j] = p[j-1];    
  19.             ++*move_cnt;   
  20.         }  
  21.         p[j] = t;    
  22.         ++*move_cnt;   
  23.     }       
  24. }    
  25.   
  26. // 希尔排序  
  27. void ShellSort(int *p, int size, int* move_cnt, int* compare_cnt)  
  28. {  
  29.     int iTemp;  
  30.   
  31.     forint iStep = size/2; iStep > 0; iStep = iStep/2 )  
  32.     {  
  33.         forint i = iStep; i < size; ++i )  
  34.         {  
  35.                
  36.             iTemp = p[i];  
  37.             ++*move_cnt;  
  38.             int j = i;  
  39.             for( ; j >= iStep; j -= iStep )  
  40.             {     
  41.                 ++*compare_cnt;  
  42.                 if( iTemp < p[j-iStep] )  
  43.                 {  
  44.                     p[j] = p[j-iStep];  
  45.                     ++*move_cnt;  
  46.                 }  
  47.                 else  
  48.                 {  
  49.                     break;  
  50.                 }  
  51.             }  
  52.             p[j] = iTemp;  
  53.             ++*move_cnt;  
  54.         }  
  55.     }  
  56. }  
  57.   
  58. // 冒泡排序  
  59. void BubbleSort(int* arr,int size, int* move_cnt, int* compare_cnt)    
  60. {    
  61.     bool cg = true;         
  62.     int  n  = 0;    
  63.     do    
  64.     {    
  65.         cg = false;     
  66.         for (int i=0;i<size-1-n;i++)    
  67.         {     
  68.             ++*compare_cnt;  
  69.             if (arr[i]>arr[i+1])    
  70.             {    
  71.                 cg = true;    
  72.                 swap(arr[i],arr[i+1]);       
  73.                 *move_cnt += 3;     
  74.             }    
  75.         }       
  76.         n++;    
  77.     } while (cg);    
  78. }    
  79.   
  80. // 快速排序  
  81. void QuickSort(int* arr,int size, int* move_cnt, int* compare_cnt)    
  82. {    
  83.     int *L,*R;    
  84.     if(size <= 1) return;    
  85.     if(2 == size)    
  86.     {    
  87.        ++*compare_cnt;  
  88.        if(arr[0] > arr[1]) {   
  89.          swap(arr[0],arr[1]);  
  90.          *move_cnt += 3;   
  91.        }    
  92.        return;    
  93.     }     
  94.     L = &arr[1];    
  95.     R = &arr[size -1];    
  96.     while(L < R)    
  97.    {    
  98.       ++*compare_cnt;  
  99.       while(L<R && *L < *arr) {  
  100.         ++*compare_cnt;  
  101.         ++L;  
  102.       }    
  103.       ++*compare_cnt;  
  104.       while(R>arr && !(*R < *arr)) {  
  105.          ++*compare_cnt;  
  106.          --R;   
  107.       }   
  108.      ++*compare_cnt;  
  109.      if(L<R){  
  110.        swap(*R,*L);  
  111.        *move_cnt += 3;   
  112.      }    
  113.    }    
  114.    swap(*R,*arr);    
  115.    *move_cnt += 3;   
  116.    QuickSort(arr, R-arr, move_cnt, compare_cnt);    
  117.    QuickSort(R+1,size -1 -(R-arr), move_cnt, compare_cnt);    
  118. }    
  119.   
  120. // 简单选择排序  
  121. void SelectSort(int* arr,int size, int* move_cnt, int* compare_cnt)    
  122. {    
  123.     for(int i=0;i<size;i++)    
  124.     {    
  125.         int psn=i;    
  126.         for(int j=i;j<size;j++)    
  127.         {      
  128.             ++*compare_cnt;  
  129.             if(arr[j]<arr[psn]) {    
  130.                psn = j;    
  131.             }  
  132.         }       
  133.         swap(arr[psn],arr[i]);    
  134.         *move_cnt += 3;   
  135.     }       
  136. }    
  137.   
  138. void Shift(int* heap, int start, int end, int* move_cnt, int* compare_cnt)    
  139. {    
  140.        int tmp = heap[start];     
  141.        ++*move_cnt;  
  142.        int parent = start;    
  143.        int child = parent * 2 + 1;     
  144.        while(child <= end)    
  145.      {    
  146.        ++*compare_cnt;  
  147.        if(child < end && heap[child] < heap[child+1]) {   
  148.          ++child;    
  149.        }  
  150.        ++*compare_cnt;  
  151.        if(tmp < heap[child])    
  152.        {    
  153.              heap[parent] = heap[child];    
  154.              ++*move_cnt;  
  155.              parent = child;    
  156.              child = 2 * parent +1;    
  157.        } else  break;    
  158.      }    
  159.     heap[parent] = tmp;    
  160.     ++*move_cnt;  
  161. }    
  162.     
  163. void BuildHeap(int* heap,int size, int* move_cnt, int* compare_cnt)    
  164. {    
  165.       for(int i=size/2-1; i >=0; i--)    
  166.          Shift(heap,i,size-1, move_cnt, compare_cnt);    
  167.     
  168. }    
  169.     
  170. void HeapSort(int* heap, int size, int* move_cnt, int* compare_cnt)    
  171. {    
  172.     BuildHeap(heap, size, move_cnt, compare_cnt);    
  173.     for(int i = size-1; i>0; i--)    
  174.     {    
  175.           swap(heap[0],heap[i]);    
  176.           Shift(heap,0,i-1, move_cnt, compare_cnt);    
  177.     }    
  178. }    
  179.   
  180. void Merge(int* arr,int first,int mid,int last, int* move_cnt, int* compare_cnt)    
  181. {    
  182.      int size = last -first + 1;    
  183.      int* tmp = new int[size];    
  184.      int   b1 = first;    
  185.      int   e1 = mid;    
  186.      int   b2 = mid + 1;    
  187.      int   e2 = last;    
  188.      int    k = 0;    
  189.   
  190.      while(b1<=e1 && b2<=e2) {   
  191.        ++*compare_cnt;  
  192.        if(arr[b1]<arr[b2])    
  193.           tmp[k++] = arr[b1++];    
  194.        else tmp[k++] = arr[b2++];    
  195.        ++*move_cnt;  
  196.      }  
  197.      while(b1<=e1) {    
  198.        tmp[k++]=arr[b1++];  
  199.        ++*move_cnt;    
  200.        ++*compare_cnt;  
  201.      }  
  202.      while(b2<=e2) {   
  203.        tmp[k++]=arr[b2++];  
  204.        ++*move_cnt;  
  205.        ++*compare_cnt;  
  206.      }    
  207.      for(int i=0; i<size; i++)  {   
  208.         arr[first + i] = tmp[i];   
  209.         ++*move_cnt;  
  210.      }   
  211.      delete tmp;       
  212. }    
  213.     
  214. void MergeSort(int* arr,int size, int* move_cnt, int* compare_cnt)    
  215. {    
  216.     if(size > 1)    
  217.     {    
  218.        int  mid = size / 2;    
  219.        MergeSort(arr, mid, move_cnt, compare_cnt);    
  220.        MergeSort(arr + mid, size - mid, move_cnt, compare_cnt);  
  221.        Merge(arr, 0, mid - 1, size -1, move_cnt, compare_cnt);    
  222.     }    
  223. }    
  224.   
  225. // 输出数组  
  226. void ShowArray(int * p, int size)  
  227. {  
  228.     for(int i = 0; i < size; i++)  
  229.     {  
  230.         cout<< p[i];  
  231.         if(i < size-1)  
  232.         {  
  233.             cout<< ", ";  
  234.         }  
  235.     }  
  236.     cout<< endl;  
  237. }  
  238.   
  239. void CopyToTmp(int* arr, int* temp, int length) {  
  240.     forint i = 0; i < length; ++i )  
  241.     {  
  242.         temp[i] = arr[i];  
  243.           
  244.     }   
  245. }  
  246.   
  247. int main()  
  248. {  
  249.     int arr[4][20] = { {-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100},  
  250.                        {100, 99, 96, 93,89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20},  
  251.                        {99, 75, -10, 87, 41,100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79},  
  252.                        {0} };  
  253.     const string arr_name[3] = {"顺序的数组" ,"逆序的数组""随机的数组"};  
  254.       
  255.     SortFunc sort_functions[7] = {InsertSort, SelectSort, BubbleSort, QuickSort, ShellSort, HeapSort, MergeSort };  
  256.     const string sort_func_name[7] = {"插入排序""选择排序""冒泡排序""快速排序""希尔排序""堆排序""归并排序" };  
  257.     int compare_cnt = 0, move_cnt = 0;  
  258.     cout <<  "========================================================" << endl;  
  259.     for (int i = 0; i < 7; ++i) {  
  260.         cout << "         **** " << sort_func_name[i] << " ****" << "       测试结果如下:" << endl ;  
  261.         cout <<  "========================================================" << endl;  
  262.       for (int j = 0; j < 3; ++j) {  
  263.           compare_cnt = 0;  
  264.           move_cnt = 0;  
  265.           CopyToTmp(arr[j], arr[3], 20);  
  266.           cout << endl << arr_name[j] << ":";   
  267.           ShowArray(arr[3], 20);  
  268.           sort_functions[i](arr[3], 20, &move_cnt, &compare_cnt);  
  269.           cout << sort_func_name[i] << "后: " ;   
  270.           ShowArray(arr[3], 20);  
  271.           cout << sort_func_name[i] << "对" << arr_name[j] << "排序,共进行 " << compare_cnt << " 次比较 和 " << move_cnt << " 次移动" << endl;   
  272.       }      
  273.     cout << endl << "========================================================" << endl;  
  274.     }   
  275.     system("pause");  
  276.     return 0;  
  277. }  


【测试结果】

[cpp]  view plain copy
  1. ========================================================  
  2.          **** 插入排序 ****       测试结果如下:  
  3. ========================================================  
  4.   
  5.   
  6. 顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  7. 插入排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  8. 插入排序对顺序的数组排序,共进行 19 次比较 和 38 次移动  
  9.   
  10.   
  11. 逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20  
  12. 插入排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  13. 插入排序对逆序的数组排序,共进行 209 次比较 和 228 次移动  
  14.   
  15.   
  16. 随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79  
  17. 插入排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  18. 插入排序对随机的数组排序,共进行 132 次比较 和 151 次移动  
  19.   
  20.   
  21. ========================================================  
  22.          **** 选择排序 ****       测试结果如下:  
  23. ========================================================  
  24.   
  25.   
  26. 顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  27. 选择排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  28. 选择排序对顺序的数组排序,共进行 210 次比较 和 60 次移动  
  29.   
  30.   
  31. 逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20  
  32. 选择排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  33. 选择排序对逆序的数组排序,共进行 210 次比较 和 60 次移动  
  34.   
  35.   
  36. 随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79  
  37. 选择排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  38. 选择排序对随机的数组排序,共进行 210 次比较 和 60 次移动  
  39.   
  40.   
  41. ========================================================  
  42.          **** 冒泡排序 ****       测试结果如下:  
  43. ========================================================  
  44.   
  45.   
  46. 顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  47. 冒泡排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  48. 冒泡排序对顺序的数组排序,共进行 19 次比较 和 0 次移动  
  49.   
  50.   
  51. 逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20  
  52. 冒泡排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  53. 冒泡排序对逆序的数组排序,共进行 190 次比较 和 570 次移动  
  54.   
  55.   
  56. 随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79  
  57. 冒泡排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  58. 冒泡排序对随机的数组排序,共进行 189 次比较 和 339 次移动  
  59.   
  60.   
  61. ========================================================  
  62.          **** 快速排序 ****       测试结果如下:  
  63. ========================================================  
  64.   
  65.   
  66. 顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  67. 快速排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  68. 快速排序对顺序的数组排序,共进行 244 次比较 和 54 次移动  
  69.   
  70.   
  71. 逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20  
  72. 快速排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  73. 快速排序对逆序的数组排序,共进行 235 次比较 和 57 次移动  
  74.   
  75.   
  76. 随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79  
  77. 快速排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  78. 快速排序对随机的数组排序,共进行 140 次比较 和 54 次移动  
  79.   
  80.   
  81. ========================================================  
  82.          **** 希尔排序 ****       测试结果如下:  
  83. ========================================================  
  84.   
  85.   
  86. 顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  87. 希尔排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  88. 希尔排序对顺序的数组排序,共进行 62 次比较 和 124 次移动  
  89.   
  90.   
  91. 逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20  
  92. 希尔排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  93. 希尔排序对逆序的数组排序,共进行 80 次比较 和 160 次移动  
  94.   
  95.   
  96. 随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79  
  97. 希尔排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  98. 希尔排序对随机的数组排序,共进行 88 次比较 和 163 次移动  
  99.   
  100.   
  101. ========================================================  
  102.          **** 堆排序 ****       测试结果如下:  
  103. ========================================================  
  104.   
  105.   
  106. 顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  107. 堆排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  108. 堆排序对顺序的数组排序,共进行 126 次比较 和 119 次移动  
  109.   
  110.   
  111. 逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20  
  112. 堆排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  113. 堆排序对逆序的数组排序,共进行 108 次比较 和 101 次移动  
  114.   
  115.   
  116. 随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79  
  117. 堆排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  118. 堆排序对随机的数组排序,共进行 118 次比较 和 108 次移动  
  119.   
  120.   
  121. ========================================================  
  122.          **** 归并排序 ****       测试结果如下:  
  123. ========================================================  
  124.   
  125.   
  126. 顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  127. 归并排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  128. 归并排序对顺序的数组排序,共进行 88 次比较 和 176 次移动  
  129.   
  130.   
  131. 逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20  
  132. 归并排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  133. 归并排序对逆序的数组排序,共进行 88 次比较 和 176 次移动  
  134.   
  135.   
  136. 随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79  
  137. 归并排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100  
  138. 归并排序对随机的数组排序,共进行 88 次比较 和 176 次移动  


            原创文章,转载请注明: 转载自IIcyZhao’s Road

          本文链接地址:http://blog.csdn.net/iicy266/article/details/11905851


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值