摘要: 希尔排序通过比较相距一定间隔的元素来工作,各趟比较所用的距离随着算法的进行而减少,直到只比较相邻元素的最后一趟排序为止。希尔排序使用一个序列h1,h2, ... , hk,叫做增量序列。在使用增量hk的一趟排序后,对于每一个i我们都有A[i] void shellsort(int v[], int n){ int gap; int i, j, temp; for (gap = n / 2; gap > 0; gap /= 2) { for (i = gap; i = 0 && v[j] > v[j + gap]; j -= gap) { temp ... 阅读全文
posted @ 2013-09-28 10:39 玩的就是 心跳 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 如其名,快速排序(quicksort)是在实践中最快的已知排序算法,平均运行时间是O(NlogN),最坏情况下为O(N^2)。像归并排序一样,它也是一种分治的递归算法。将数组S排序的基本算法由如下4步组成:1. 若S中元素个数是0或1,则返回。2. 取S中任一元素v,称之为枢纽元(pivot)。最好的选择是取S的中值。3. 将S中除去枢纽元其余元素分成两个子集,一个子集S1中的所有元素都比枢纽元小,另一个子集S2中元素都比枢纽元大。4. 分别调用quicksort(S1),quicksort(S2)。《C程序设计语言》一书中将这个算法作为讲解递归的例子,很经典,代码如下:#includ... 阅读全文
posted @ 2013-09-27 18:25 玩的就是 心跳 阅读(221) 评论(0) 推荐(0) 编辑
摘要: #include #include typedef struct node{ int id; struct node *left; struct node *right;} node; node *find(int id, node *root){ if (root == NULL) return (NULL); if (id id) return (find(id, root->left)); else if (id > root->id) return (find(id, root->right)); else ... 阅读全文
posted @ 2013-09-25 16:53 玩的就是 心跳 阅读(230) 评论(0) 推荐(0) 编辑
摘要: #include #include #define max(x, y) (((x) > (y)) ? (x) : (y))typedef struct node{ int id; int h; // height struct node *left; struct node *right;} node;static node *findmin(node *root){ if (root == NULL) return (NULL); if (root->left == NULL) return (root); ... 阅读全文
posted @ 2013-09-25 16:47 玩的就是 心跳 阅读(95) 评论(0) 推荐(0) 编辑
摘要: #include #include typedef struct node{ int id; struct node *next;} node;node *reverse(node *head){ node *reverse_head; node *n; node *p = NULL; node *c = head; while (c != NULL) { n = c->next; if (n == NULL) reverse_head = c; c->next = p; p = c; c = n; } ... 阅读全文
posted @ 2013-09-24 13:06 玩的就是 心跳 阅读(284) 评论(0) 推荐(0) 编辑
摘要: 1 #include 2 #include 3 4 typedef struct node 5 { 6 int id; 7 struct node * next; 8 } node; 9 10 node *merge(node *head1, node *head2)11 {12 node *pa = head1;13 node *pb = head2;14 node *head;15 node a; 16 node *pc = &a;17 18 if (pa == NULL)19 return pb;20 if (pb ==... 阅读全文
posted @ 2013-09-24 09:55 玩的就是 心跳 阅读(163) 评论(0) 推荐(0) 编辑