现在的位置: 首页 > 综合 > 正文

计数排序

2014年03月20日 ⁄ 综合 ⁄ 共 2349字 ⁄ 字号 评论关闭
  1. /*
  2.  * 算法名称:计数排序
  3.  * 算法思想:在最后排好序的序列中,第j个键码恰恰大于(j-1)个其它键码.
  4.  *           比如:如果知道某个键码确实超过7个其它键码,而且没有两个键
  5.  *           码相同,刚在排序之后对应的记录应当进入位置8.
  6.  * 算法实现:
  7.  *           通过维护一张辅助表count[1]...count[N],对于小于一个给定键
  8.  *           码的键码个数进行计数,实现用键码K1,...Kn对记录R1,...,Rn排
  9.  *           序. 算法结束时,count[j]+1确定记录Rj的最后位置.
  10.  */
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <stdlib.h>     /* for rand() and srand() */
  14. #define N 10            /* the number of elements */
  15. //@param: 
  16. //array: to be sorted array.
  17. int *count_sort(int *array);
  18. //function:
  19. //          initial the array with integer.
  20. //@param:
  21. //array: to be sorted array.
  22. void init_rand(int *array);
  23. //function: print the elements of the array.
  24. //@param:
  25. //array: to be sorted array.
  26. void print_array(int *array);
  27. int main(void)
  28. {
  29.   int array[N];
  30.   init_rand(array);
  31.   printf("The array has been initialed as follows:/n");
  32.   print_array(array);
  33.   count_sort(array);
  34.   printf("After count sort:/n");
  35.   print_array(array);
  36.   return 0;
  37. }//main()
  38. void init_rand(int *array)
  39. {
  40.   int i;
  41.   srand((unsigned)time(NULL));
  42.   for (i = 0; i < N; i++)
  43.   {
  44.     array[i] = rand() % 900 + 100;     /* initialize the array between 100 and 999 */
  45.   }//for
  46. }//init_rand()
  47. void print_array(int *array)
  48. {
  49.   int i;
  50.   for (i = 0; i < N; i++)
  51.   {
  52.     printf("%d/t", array[i]);
  53.   }//for
  54.   printf("/n");
  55. }//print_array()
  56. int *count_sort(int *array)
  57. {
  58.   int i,j;
  59.   int count[N] = {0};
  60.   int temp[N] = {0};
  61.   for (i = 1; i < N; i++)
  62.   {
  63.     for (j = 0; j < i; j++)
  64.     {
  65.       array[i] < array[j] ? count[j]++ : count[i]++;
  66.     }//for
  67.   }//for
  68.   printf("Now, the elements' value of count are as follows:/n");
  69.   print_array(count);
  70.   for (i = 0; i < N; i++)
  71.   {
  72.     temp[i] = array[i];
  73.   }//for
  74.   
  75.   for (i = 0; i < N; i++)
  76.   {
  77.     array[count[i]] = temp[i];
  78.   }//for
  79.   return array;
  80. }//count_sort()
  81. //运行情况;
    1. lee@Lee:~/MyProject/sort/count_sort$ ./count_sort 
    2. The array has been initialed as follows:
    3. 418 292 332 357 709 779 336 103 503 802 
    4. Now, the elements' value of count are as follows:
    5. 5   1   2   4   7   8   3   0   6   9   
    6. After count sort:
    7. 103 292 332 336 357 418 503 709 779 802 
    8. --------------------------------------------------------------------
    9. lee@Lee:~/MyProject/sort/count_sort$ ./count_sort 
    10. The array has been initialed as follows:
    11. 486 158 181 688 235 687 566 420 185 747 
    12. Now, the elements' value of count are as follows:
    13. 5   0   1   8   3   7   6   4   2   9   
    14. After count sort:
    15. 158 181 185 235 420 486 566 687 688 747 

抱歉!评论已关闭.