对c语言系统库函数、堆排序、希尔排序、折半插入排序、快速排序消耗时间的比较

  1. #include <stdio.h>  
  2. #include <time.h>  
  3.   
  4. #define N 100000  
  5.   
  6. /*库比较函数:qsort(int *base,int n,int struct_size,int (*compare)(const  
  7.  
  8. void *,const void *))中的比较函数*/  
  9. int compare(const void *first, const void *second)  
  10. {  
  11.     if (*(int *)first > *(int *)second)/*当第一个数比第二个数大的时候, 
  12.  
  13. 就进行排序,排序的结果是从小到大*/  
  14.         return 1;  
  15.     else  
  16.         return -1;  
  17. }  
  18.   
  19.   
  20. /*希尔排序*/  
  21. void ShellInsert(int *a, int n, int d)  
  22. {  
  23.     int i, j, temp;  
  24.     for (i = d; i < n; ++i){  
  25.         j = i - d;  
  26.         temp = a[i];  
  27.         while (j >= 0 && temp < a[j]){  
  28.             a[j + d] = a[j];  
  29.             j -= d;  
  30.         }  
  31.         a[j + d] = temp;  
  32.     }  
  33. }  
  34.   
  35. void ShellSort(int *a, int n)  
  36. {  
  37.     int d = n / 2;  
  38.     while (d >= 1){  
  39.         ShellInsert(a,n,d);  
  40.         d /= 2;  
  41.     }  
  42. }  
  43.   
  44. /*快速排序*/  
  45.   
  46. void QuickSort(int *a,int low,int high)  
  47. {  
  48.     int l = low, h = high, temp;  
  49.     if (l < h){  
  50.         temp = a[low];  
  51.         while (l != h){  
  52.             while (l<h&&a[h]>temp)  
  53.                 --h;  
  54.             if (l < h){  
  55.                 a[l] = a[h];  
  56.                 ++l;  
  57.             }  
  58.             while (l < h&&a[l] < temp)  
  59.                 ++l;  
  60.             if (l < h){  
  61.                 a[h] = a[l];  
  62.                 --h;  
  63.             }  
  64.         }  
  65.         a[l] = temp;  
  66.         QuickSort(a,low,l-1);  
  67.         QuickSort(a,h+1,high);  
  68.     }  
  69. }  
  70.   
  71. /*堆排序*/  
  72.   
  73. void HeapInsert(int *a, int low, int high)  
  74. {  
  75.     int i = low, j = low * 2, temp = a[i];  
  76.     while (j <= high){  
  77.         if (j < high && a[j] < a[j + 1])  
  78.             ++j;  
  79.         if (temp < a[j]){  
  80.             a[i] = a[j];  
  81.             i = j;  
  82.             j *= 2;  
  83.         }  
  84.         else  
  85.             break;  
  86.     }  
  87.     a[i] = temp;  
  88. }  
  89.   
  90. void HeapSort(int *a, int n)  
  91. {  
  92.     int i, temp;  
  93.     for (i = n / 2; i >= 0; --i)  
  94.         HeapInsert(a,i,n-1);  
  95.     for (i = n - 1; i >= 1; --i){  
  96.         temp = a[i];  
  97.         a[i] = a[0];  
  98.         a[0] = temp;  
  99.         HeapInsert(a,0,i-1);  
  100.     }  
  101. }  
  102.   
  103. /*折半排序*/  
  104.   
  105. void BinarySort(int *a,int n)  
  106. {  
  107.     int i, j, low, middle, high, temp;  
  108.     for (i = 1; i < n; ++i){  
  109.         low = 0;  
  110.         high = i - 1;  
  111.         temp = a[i];  
  112.         while (low <= high){  
  113.             middle = (low + high) / 2;  
  114.             if (temp < a[middle])  
  115.                 high = middle - 1;  
  116.             else  
  117.                 low = middle + 1;  
  118.         }  
  119.         for (j = i - 1; j > high; --j)  
  120.             a[j + 1] = a[j];  
  121.         a[low] = temp;  
  122.     }  
  123. }  
  124.   
  125. void initArray(int *a, int n)  
  126. {  
  127.     srand(time(NULL));  
  128.     int i;  
  129.     for (i = 0; i < n; ++i)  
  130.         a[i] = rand() % 100;  
  131. }  
  132.   
  133. void print(int *a,int n)  
  134. {  
  135.     int i;  
  136.     for (i = 0; i < n; ++i){  
  137.         printf("%d ", a[i]);  
  138.         if (i && i % 20 == 0)  
  139.             printf("\n");  
  140.     }  
  141.     printf("\n");  
  142. }  
  143.   
  144. int main()  
  145. {  
  146.     int a[N];  
  147.     double start, finish;  
  148.   
  149.     //initArray(a, N);  
  150.     //printf("排序前:\n");  
  151.     //print(a, N);/*排序前*/  
  152.   
  153.     /*计算系统库函数排序消耗的时间*/  
  154.     initArray(a, N);  
  155.     start = clock();  
  156.     qsort(a, N, sizeof(int), compare);  
  157.     finish = clock();  
  158.     printf("System library sort cost %.3fSeconds\n", (finish - start) /   
  159.   
  160. 1000);  
  161.     /*计算折半插入排序消耗的时间*/  
  162.     initArray(a, N);  
  163.     start = clock();  
  164.     BinarySort(a,N);  
  165.     finish = clock();  
  166.     printf("Binary sort cost %.3fSeconds\n", (finish - start) / 1000);  
  167.     /*计算堆排序消耗的时间*/  
  168.     initArray(a, N);  
  169.     start = clock();  
  170.     HeapSort(a, N);  
  171.     finish = clock();  
  172.     printf("Heap sort cost %.3fSeconds\n", (finish - start) / 1000);  
  173.     /*计算希尔排序消耗的时间*/  
  174.     initArray(a, N);  
  175.     start = clock();  
  176.     ShellSort(a, N);  
  177.     finish = clock();  
  178.     printf("Shell sort cost %.3fSeconds\n", (finish - start) / 1000);  
  179.     /*计算快速排序消耗的时间*/  
  180.     initArray(a, N);  
  181.     start = clock();  
  182.     QuickSort(a, 0, N - 1);  
  183.     finish = clock();  
  184.     printf("Quick sort cost %.3fSeconds\n", (finish - start) / 1000);  
  185.       
  186.     //printf("排序后:\n");  
  187.     //print(a, N);  
  188.     return 0;  
  189. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值