Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29700
  • 博文数量: 6
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 116
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-28 14:21
文章分类
文章存档

2015年(1)

2014年(2)

2013年(3)

我的朋友

分类: 高性能计算

2015-12-06 14:58:31

今天还得洗衣服洗澡洗袜子。。。 就单纯的列出了这两种算法,但是没有具体解释第二种为什么会比第一种快。改天再给大家讲解原因。。。。代码写完之后也没有review,要是有什么问题麻烦大牛指正。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include<string.h>
  4. #include<assert.h>


  5. //the total number need to be sort
  6. #define MAXNUMBER            10000

  7. // the max numbers
  8. #define SORTNUMBER            32


  9. typedef struct sortnode_t{
  10.     int data;
  11.     //_sortnode *pre;
  12.     struct sortnode_t *next;
  13. }sortnode;

  14. //the array need to be sort
  15. static int randomData[MAXNUMBER];

  16. //sorted array
  17. static int sortRandomData[SORTNUMBER];


  18. //init the array which is need to be sort
  19. void initRandomData(void)
  20. {
  21.     unsigned int i =0;
  22.     srand((unsigned)time(NULL));
  23.     memset(randomData,0,sizeof(randomData));
  24.     for(i = 0; i < MAXNUMBER; i ++)
  25.     {
  26.         randomData[i] = rand();
  27.     }

  28. }

  29. //sort the inited array,and find the SORTNUMBER max numbers
  30. //if MAXNUMBER is large,this way will spend lots of time
  31. void sortData_theWorst(void)
  32. {
  33.     //should not change the org array(randomData)
  34.     int *tmpptr;
  35.     int i,j;
  36.     tmpptr = malloc(sizeof(randomData));
  37.     memcpy(tmpptr, randomData, MAXNUMBER * sizeof(int));
  38.     
  39.     //init sortRandomData
  40.     memset(sortRandomData, 0, sizeof(sortRandomData));
  41.     for(i = 0; i < SORTNUMBER; i++)
  42.     {
  43.         sortRandomData[i] = tmpptr[i];
  44.         for(j = i; j < MAXNUMBER; j ++)
  45.         {
  46.             if(sortRandomData[i] < tmpptr[j])
  47.             {
  48.                 sortRandomData[i] = tmpptr[j];
  49.                 tmpptr[j] =tmpptr[i];
  50.                 tmpptr[i] =sortRandomData[i];
  51.             }
  52.         }
  53.     }
  54.     //print the result
  55.     for(i = 0; i < SORTNUMBER; i++)
  56.     {
  57.         if((i & 0xF0 == 0) && (i !=0))
  58.         {
  59.             printf("\n");
  60.         }

  61.         printf("%d\t", sortRandomData[i]);
  62.     }
  63.     printf("\n");


  64. }

  65. //insert the value to the right list, return the min value of the list
  66. int insertNodeToList(sortnode **list, int value)
  67. {
  68.     int i,nodenumber,mindata;
  69.     sortnode *p;
  70.     sortnode *tmplist;
  71.     tmplist= malloc(sizeof(sortnode));
  72.     assert(tmplist != NULL);
  73.     tmplist->data = value;
  74.     tmplist ->next = NULL;
  75.     p = *list;
  76.     for(i = 0; i < SORTNUMBER; i ++)
  77.     {
  78.         if(value > p->data)
  79.         {
  80.             nodenumber = i;
  81.             break;
  82.         }
  83.         else
  84.         {
  85.             p = p->next;
  86.         }
  87.     }
  88.     //if i = 32,should check the source
  89.     assert(i != 32);

  90.     p = *list;
  91.     if(nodenumber == 0)
  92.     {
  93.         tmplist->next = p;
  94.         *list = tmplist;
  95.         p = *list;
  96.         nodenumber = 1;
  97.     }
  98.     else
  99.     {
  100.         for(i = 0; i < nodenumber - 1; i ++)
  101.         {
  102.             p = p->next;
  103.         }    
  104.         tmplist->next = p->next;
  105.         p->next = tmplist;
  106.     }
  107.     for(i = nodenumber; i < SORTNUMBER; i ++)
  108.     {
  109.         p = p->next;        
  110.     }
  111.     // p does not point to the last node
  112.     mindata = p->data;

  113.     assert(p->next != NULL);
  114.     free(p->next);
  115.     p->next = NULL;
  116.     return mindata;

  117. }

  118. void sortData_worse(void)
  119. {
  120.     int i,j,minof32;
  121.     sortnode *sortlist;
  122.     sortnode *tmplist;
  123.     sortnode *p;

  124.     //consider the first 32 number of randomData are the max numbers
  125.     memset(sortRandomData,0,sizeof(sortRandomData));
  126.     memcpy(sortRandomData,randomData,sizeof(int) * SORTNUMBER);
  127.     
  128.     //sort the 32 numbers
  129.     for(i = 0; i < SORTNUMBER - 1; i++)
  130.     {
  131.         int t;
  132.         for(j = i + 1; j < SORTNUMBER; j ++)
  133.         {
  134.             if(sortRandomData[i] < sortRandomData[j])
  135.             {
  136.                 t = sortRandomData[j];
  137.                 sortRandomData[j] = sortRandomData[i];
  138.                 sortRandomData[i] = t;
  139.             }    
  140.         }    
  141.     }

  142.     //init the sortlist,and fill it with the max 32 numbers
  143.     sortlist = malloc(sizeof(sortnode));
  144.     assert(sortlist != NULL);
  145.     sortlist->data = sortRandomData[0];
  146.     sortlist->next = NULL;
  147.     p = sortlist;

  148.     for(i = 1; i < SORTNUMBER; i ++)
  149.     {
  150.         tmplist= malloc(sizeof(sortnode));
  151.         assert(tmplist != NULL);
  152.         tmplist->data = sortRandomData[i];
  153.         tmplist ->next = NULL;
  154.         p->next = tmplist;
  155.         p = p ->next;
  156.     }
  157.     minof32 = randomData[SORTNUMBER - 1];
  158.     for(i = 32; i < MAXNUMBER; i++)
  159.     {
  160.         if(randomData[i] > minof32)
  161.         {
  162.             minof32 = insertNodeToList(&sortlist, randomData[i]);        
  163.         }
  164.     }

  165.     p = sortlist;
  166.     for(i = 0; i < SORTNUMBER; i++)
  167.     {
  168.         if((i & 0xF0 == 0) && (i != 0))
  169.         {
  170.             printf("\n");
  171.         }

  172.         printf("%d\t", p->data);
  173.         p = p->next;
  174.     }
  175.     printf("\n");




  176. }

  177. void main(void)
  178. {
  179.     int i;
  180.     initRandomData();
  181.     
  182.     
  183.     sortData_theWorst();


  184.     printf("the second way\n");
  185.     sortData_worse();

  186.     
  187. }


阅读(365) | 评论(0) | 转发(0) |
0

上一篇:samba服务器配置

下一篇:没有了

给主人留下些什么吧!~~