Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244788
  • 博文数量: 62
  • 博客积分: 973
  • 博客等级: 准尉
  • 技术积分: 530
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-16 23:25
文章分类

全部博文(62)

文章存档

2013年(1)

2012年(14)

2011年(47)

分类: C/C++

2011-12-27 22:55:11

  1. #include "stdafx.h"
  2. #include <stdlib.h>

  3. typedef int DATA_TYPE;

  4. typedef struct _node
  5. {
  6.     DATA_TYPE data;
  7.     struct _node *next;
  8. }node_t;
  9. typedef node_t *LINKLIST;

  10. void show_list(LINKLIST head);

  11. LINKLIST create_list(int size)
  12. {
  13.     int i = 0;
  14.     LINKLIST head;
  15.     node_t *p, *r;

  16.     head = (node_t *)malloc(sizeof(node_t));
  17.     p = r = head;
  18.     printf("create an int list with size of %d:\n", size);
  19.     while( ++i <= size )
  20.     {
  21.         p = (node_t *)malloc( sizeof(node_t) );
  22.         p->data = rand()%1000;
  23.         r->next = p;
  24.         r = p;
  25.         r->next = NULL;
  26.     }
  27.     
  28.     return head;
  29. }

  30. LINKLIST merge_link(node_t *pa, node_t *pb)
  31. {
  32.     LINKLIST head;
  33.     node_t *pr;

  34.     head = (node_t *)malloc( sizeof(node_t) );
  35.     head->next = NULL;
  36.     pr = head;

  37.     while(pa && pb)
  38.     {
  39.         if(pa->data <= pb->data)
  40.         {
  41.             pr->next = pa;
  42.             pr = pa;
  43.             pa = pa->next;
  44.         }
  45.         else
  46.         {
  47.             pr->next = pb;
  48.             pr = pb;
  49.             pb = pb->next;
  50.         }
  51.     }
  52.     pr->next = pa ? pa : pb;

  53.     return head->next;
  54. }

  55. node_t* mergeSort(node_t *alist)
  56. {
  57.     node_t *first;
  58.     node_t *second;
  59.     first = alist;
  60.     second = alist->next;

  61.     if(NULL == first || NULL == second)
  62.     {    
  63.         return alist; //only one node left
  64.     }
  65.     //devide alist to 2 lists by middle
  66.     while(second != NULL && second->next != NULL)
  67.     {
  68.         alist = alist->next;
  69.         second = second->next->next;
  70.     }
  71.     second = alist->next;//(alist.len + 1)/2
  72.     alist->next = NULL;

  73.     return merge_link( mergeSort(first), mergeSort(second) );
  74. }

  75. void show_list(LINKLIST head)
  76. {
  77.     node_t *p = head;
  78.     //p = head->next;
  79.     while(p)
  80.     {
  81.         printf("%d", p->data);
  82.         p = p->next;
  83.         if(p) printf("->");
  84.     }
  85.     printf("\n");
  86. }


  87. int _tmain(int argc, _TCHAR* argv[])
  88. {
  89.     int n;
  90.     node_t *plist;
  91.     node_t *psort;

  92.     printf("Please input the length of link:\n");
  93.     scanf("%d", &n);
  94.     plist = create_list( n );// with head
  95.     show_list(plist->next); // without head

  96.     psort = mergeSort(plist->next);// without head
  97.     printf("after merge sort:\n");
  98.     show_list(psort);

  99.     return 0;
  100. }
    “堆排序、快速排序这些在数组排序时性能非常好的算法,在链表只能“顺序访问”的限制下无法施展魔力;但是归并排序却如鱼得水,非但保持了它O(nlogn)的时间复杂度,而且它在数组排序中广受诟病的空间复杂度在链表排序中也从O(n)降到了O(1)。真是好得不得了啊,哈哈。”
 
阅读(1824) | 评论(0) | 转发(0) |
0

上一篇:位图表示法

下一篇:全排列和组合问题

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