1. /* List Insertion Sorting.
  2. * Implementation history:.
  3. * 2013-09-15, Mars Fu, first version.
  4. */
  5.  
  6. #include "stdafx.h"
  7.  
  8. #include "list_insertion.h"
  9.  
  10. int
  11. init_list(struct s_clist *hd, int max_cnt, struct s_nodes *nodes)
  12. {
  13. if (hd == NULL) return 0;
  14. if (max_cnt < 0) return 0;
  15.  
  16. hd->node = NULL;
  17.  
  18. nodes->cur = hd;
  19. nodes->hd = hd;
  20. memset(nodes->cur, 0, sizeof(struct s_clist));
  21. nodes->nodes_cnt = 0;
  22. nodes->max_nodes_cnt = max_cnt;
  23.  
  24. debug("nodes->max_nodes_cnt %d \r\n", nodes->max_nodes_cnt);
  25.  
  26. return 1;
  27. }
  28.  
  29. int
  30. insert_list_sort(struct s_node *node, struct s_nodes *nodes, cmp_func cmp)
  31. {
  32. struct s_clist *tmp;
  33. struct s_clist *next;
  34.  
  35. if (node == NULL) return 0;
  36. if (nodes->cur == NULL) return 0;
  37.  
  38. next = (struct s_clist *)malloc(sizeof(struct s_clist));
  39. if (next == NULL) {
  40. debug("malloc list failed \r\n");
  41. return 0;
  42. }
  43.  
  44. next->node = node;
  45.  
  46. tmp = nodes->hd->pre;
  47.  
  48. if (tmp == NULL) {
  49. tmp = nodes->hd;
  50. tmp->next = next;
  51. next->pre = tmp;
  52. next->next = nodes->hd;
  53. nodes->hd->pre = next;
  54. }
  55. else {
  56.  
  57. while (tmp != NULL) {
  58. if (tmp->node == NULL) break;
  59. if (cmp(tmp->node->key, node->key) < 0) break;
  60. tmp = tmp->pre;
  61. }
  62.  
  63. next->next = tmp->next;
  64. tmp->next->pre = next;
  65. tmp->next = next;
  66. next->pre = tmp;
  67. }
  68.  
  69. nodes->cur = nodes->hd->pre;
  70. nodes->nodes_cnt++;
  71.  
  72. return 1;
  73. }
  74.  
  75. int
  76. is_list_full(struct s_nodes *nodes)
  77. {
  78. return (nodes->nodes_cnt >= nodes->max_nodes_cnt);
  79. }
  80.  
  81. int
  82. get_list_cnt(struct s_nodes *nodes)
  83. {
  84. return nodes->nodes_cnt;
  85. }
  86.  
  87. int
  88. delete_item_from_list(struct s_node *node, struct s_nodes *nodes)
  89. {
  90. struct s_clist *cur;
  91.  
  92. if(node == NULL) return 0;
  93.  
  94. cur = nodes->hd;
  95. cur = cur->next;
  96. while(cur->node != NULL) {
  97. if(cur->node != node) {
  98. cur = cur->next;
  99. continue;
  100. }
  101.  
  102. cur->pre->next = cur->next;
  103. cur->next->pre = cur->pre;
  104.  
  105. cur->next = NULL;
  106. cur->pre = NULL;
  107. free(cur->node);
  108. free(cur);
  109.  
  110. nodes->nodes_cnt--;
  111.  
  112. return 1;
  113. }
  114.  
  115. return 0;
  116. }
  117.  
  118. #ifdef LIST_INSERTION_DEBUG
  119.  
  120. static int
  121. int_increasing_cmp(void* lv, void* rv)
  122. {
  123. int tmp_lv, tmp_rv;
  124.  
  125. tmp_lv = *(int*)lv;
  126. tmp_rv = *(int*)rv;
  127.  
  128. return (tmp_lv - tmp_rv);
  129. }
  130.  
  131. static void
  132. debug_func(void*src, int n, int h)
  133. {
  134. int i;
  135.  
  136. debug("%d-sort:\r\n----\r\n", h);
  137. for (i = 0; i < n; ++i) {
  138. debug("%d ", *((int*)src + i));
  139. }
  140. debug("\r\n----\r\n");
  141. }
  142.  
  143. int
  144. main(int argc, char* argv[])
  145. {
  146. int i;
  147. int cnt;
  148. const int int_items[] = { 503, 87, 512, 61, 908, 170, 897, 275,
  149. 653, 426, 154, 509, 612, 677, 765, 703
  150. };
  151. int ret;
  152. struct s_clist hd;
  153. struct s_nodes nodes;
  154. struct s_node *node;
  155. struct s_clist *cur;
  156.  
  157. debug("[Testing list insertion sort].. \r\n");
  158.  
  159. ret = init_list(&hd, 1000, &nodes);
  160. if (!ret) goto END;
  161.  
  162. cnt = sizeof(int_items)/sizeof(int_items[0]);
  163.  
  164. debug("src database:\r\n----\r\n");
  165. for (i = 0; i < cnt; ++i) {
  166. debug("%d ", int_items[i]);
  167. }
  168. debug("\r\n----\r\n");
  169.  
  170. for (i = 0; i < cnt; ++i) {
  171. node = (struct s_node*)malloc(sizeof(struct s_node));
  172. if (node == NULL) goto END;
  173.  
  174. node->key = (void*)(&int_items[i]);
  175. ret = insert_list_sort(node, &nodes, int_increasing_cmp);
  176. if (!ret) {
  177. debug("failed. \r\n");
  178. goto END;
  179. }
  180. }
  181.  
  182. debug("dst database:\r\n----\r\n");
  183. cur = nodes.hd->next;
  184. while(cur->node != NULL) {
  185. debug("%d ", *(int*)cur->node->key);
  186. cur = cur->next;
  187. }
  188.  
  189. debug("\r\n----\r\n");
  190.  
  191. debug("\r\n");
  192.  
  193. debug("[Testing list insertion sort].. done. \r\n");
  194.  
  195. END:
  196. while(1);
  197. return 0;
  198. }
  199.  
  200. #endif /* LIST_INSERTION_DEBUG */
  1. #ifndef __LIST_INSERTION_H__
  2. #define __LIST_INSERTION_H__
  3.  
  4. #define LIST_INSERTION_DEBUG
  5.  
  6. typedef int(*cmp_func)(void*, void*);
  7. typedef void (*dbg_func)(void*, int, int);
  8.  
  9. struct s_node{
  10. void* key;
  11.  
  12. void *data;
  13. };
  14.  
  15. struct s_clist{
  16. struct s_clist *pre;
  17. struct s_clist *next;
  18. struct s_node *node;
  19. };
  20.  
  21. struct s_nodes
  22. {
  23. struct s_clist *hd;
  24. int nodes_cnt;
  25. int max_nodes_cnt;
  26.  
  27. struct s_clist *cur;
  28. };
  29.  
  30. int init_list(struct s_clist *hd, int max_cnt, struct s_nodes *nodes);
  31.  
  32. int insert_list_sort(struct s_node *node, struct s_nodes *nodes);
  33.  
  34. int is_list_full(struct s_nodes *nodes);
  35.  
  36. int get_list_cnt(struct s_nodes *nodes);
  37.  
  38. int delete_item_from_list(struct s_node *node, struct s_nodes *nodes);
  39.  
  40. #endif /* __LIST_INSERTION_H__ */
  1. #pragma once
  2.  
  3. #include <windows.h>
  4. #ifdef _WIN32
  5. #define msleep(x) Sleep(x)
  6. #endif
  7.  
  8. #include <stdio.h>
  9. #include <tchar.h>
  10. #include <stdlib.h>
  11. #include <malloc.h>
  12. #include <string.h>
  13. #include <math.h>
  14.  
  15. #define MY_DEBUG
  16. #ifdef MY_DEBUG
  17. #define debug printf
  18. #else
  19. #define debug(x,argc, __VA_ARGS__) ;
  20. #endif /* MY_DEBUG */
  21.  
  22. #define F_S() debug("[%s]..\r\n", __FUNCTION__)
  23. #define F_E() debug("[%s]..done. \r\n", __FUNCTION__)

Enjoy :)

Mars

Sep 15th, 2013

Foundation Sorting: Single List Insertion Sort的更多相关文章

  1. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  2. Codeforces Round #212 (Div. 2) C. Insertion Sort

    C. Insertion Sort Petya is a beginner programmer. He has already mastered the basics of the C++ lang ...

  3. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  4. Foundation Sorting: Quicksort

    /* Quick Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ /* [Quicksort ...

  5. 《算法4》2.1 - 插入排序算法(Insertion Sort), Python实现

    排序算法列表电梯: 选择排序算法:详见 Selection Sort 插入排序算法(Insertion Sort):非常适用于小数组和部分排序好的数组,是应用比较多的算法.详见本文 插入排序算法的语言 ...

  6. 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

    Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  8. [Algorithms] Insertion sort algorithm using TypeScript

    Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...

  9. 洛谷 SP9722 CODESPTB - Insertion Sort

    洛谷 SP9722 CODESPTB - Insertion Sort 洛谷传送门 题目描述 Insertion Sort is a classical sorting technique. One ...

随机推荐

  1. virtualBox文件共享

    具体过程,可以参考: http://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html 共享命令:sudo mount -t vboxsf ...

  2. 通过springmvc的RequestMapping的headers属性的使用

    直接上图: springmvc中可以通过@RequestMapping注解折配置headers属性,也就是通过headers属性来配置请求头信息,从而通过这个属性值来映射请求,因为不同浏览器的Acce ...

  3. 1分钟搞定超慢SQL

    前几天,一个用户的研发人员找到我了,说他们有个SQL语句非常慢,我说多慢?他们说:半个小时也没出结果.于是问他们要了SQL语句和执行计划,SQL语句就不能再这里贴出来了,下面是调整前的执行计划(略去某 ...

  4. Google市场推广统计

    Google Play作为Android最大的应用市场,也存在这推广等常用的行为,那么如何统计呢,Google Analytics SDK或者其他的SDK都提供了方法,实际上是可以不需要任何sdk,完 ...

  5. ZOJ 38727(贪心)

    这道题真心坑.越想越远  想的飞起来了. 最后纠结起后缀表达式的定义来了. 题意: 就是给你一个串 ,  让你用最少改动次数来实它变成一个合法的后缀表达式,  改动方式有两种, 一种是直接加入数字或者 ...

  6. javascript操作HTML元素

    操作 HTML 元素 如需从 JavaScript 访问某个 HTML 元素,您可以使用 document.getElementById(id) 方法. 请使用 "id" 属性来标 ...

  7. ASPxGridView后台获取edit、delete、选择框等按钮。

    GridViewCommandColumn commandColumn = (GridViewCommandColumn)ASPxGridViewInstance.Columns["#&qu ...

  8. 快速检查SQL两表数据是否一致

    1前话 项目内实现了一新功能:克隆数据库. 2目标 克隆并非用SQLSERVER克隆,故完毕后需要检查各表内一些数据与原表一致性.一些表中的某一些列容许不一致. 3实现 将两表的需要检查的几列取出,相 ...

  9. 注解方式实现Spring声明式事务管理

    1.编写实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() { re ...

  10. list根据所存对象属性排序

    比如有个list,里面存的是一个个对象,对象有个list属性,其值可以是字符串和数字. private void getSortList(List<AclResource> newList ...