/* List Insertion Sorting.
* Implementation history:.
* 2013-09-15, Mars Fu, first version.
*/ #include "stdafx.h" #include "list_insertion.h" int
init_list(struct s_clist *hd, int max_cnt, struct s_nodes *nodes)
{
if (hd == NULL) return 0;
if (max_cnt < 0) return 0; hd->node = NULL; nodes->cur = hd;
nodes->hd = hd;
memset(nodes->cur, 0, sizeof(struct s_clist));
nodes->nodes_cnt = 0;
nodes->max_nodes_cnt = max_cnt; debug("nodes->max_nodes_cnt %d \r\n", nodes->max_nodes_cnt); return 1;
} int
insert_list_sort(struct s_node *node, struct s_nodes *nodes, cmp_func cmp)
{
struct s_clist *tmp;
struct s_clist *next; if (node == NULL) return 0;
if (nodes->cur == NULL) return 0; next = (struct s_clist *)malloc(sizeof(struct s_clist));
if (next == NULL) {
debug("malloc list failed \r\n");
return 0;
} next->node = node; tmp = nodes->hd->pre; if (tmp == NULL) {
tmp = nodes->hd;
tmp->next = next;
next->pre = tmp;
next->next = nodes->hd;
nodes->hd->pre = next;
}
else { while (tmp != NULL) {
if (tmp->node == NULL) break;
if (cmp(tmp->node->key, node->key) < 0) break;
tmp = tmp->pre;
} next->next = tmp->next;
tmp->next->pre = next;
tmp->next = next;
next->pre = tmp;
} nodes->cur = nodes->hd->pre;
nodes->nodes_cnt++; return 1;
} int
is_list_full(struct s_nodes *nodes)
{
return (nodes->nodes_cnt >= nodes->max_nodes_cnt);
} int
get_list_cnt(struct s_nodes *nodes)
{
return nodes->nodes_cnt;
} int
delete_item_from_list(struct s_node *node, struct s_nodes *nodes)
{
struct s_clist *cur; if(node == NULL) return 0; cur = nodes->hd;
cur = cur->next;
while(cur->node != NULL) {
if(cur->node != node) {
cur = cur->next;
continue;
} cur->pre->next = cur->next;
cur->next->pre = cur->pre; cur->next = NULL;
cur->pre = NULL;
free(cur->node);
free(cur); nodes->nodes_cnt--; return 1;
} return 0;
} #ifdef LIST_INSERTION_DEBUG static int
int_increasing_cmp(void* lv, void* rv)
{
int tmp_lv, tmp_rv; tmp_lv = *(int*)lv;
tmp_rv = *(int*)rv; return (tmp_lv - tmp_rv);
} static void
debug_func(void*src, int n, int h)
{
int i; debug("%d-sort:\r\n----\r\n", h);
for (i = 0; i < n; ++i) {
debug("%d ", *((int*)src + i));
}
debug("\r\n----\r\n");
} int
main(int argc, char* argv[])
{
int i;
int cnt;
const int int_items[] = { 503, 87, 512, 61, 908, 170, 897, 275,
653, 426, 154, 509, 612, 677, 765, 703
};
int ret;
struct s_clist hd;
struct s_nodes nodes;
struct s_node *node;
struct s_clist *cur; debug("[Testing list insertion sort].. \r\n"); ret = init_list(&hd, 1000, &nodes);
if (!ret) goto END; cnt = sizeof(int_items)/sizeof(int_items[0]); debug("src database:\r\n----\r\n");
for (i = 0; i < cnt; ++i) {
debug("%d ", int_items[i]);
}
debug("\r\n----\r\n"); for (i = 0; i < cnt; ++i) {
node = (struct s_node*)malloc(sizeof(struct s_node));
if (node == NULL) goto END; node->key = (void*)(&int_items[i]);
ret = insert_list_sort(node, &nodes, int_increasing_cmp);
if (!ret) {
debug("failed. \r\n");
goto END;
}
} debug("dst database:\r\n----\r\n");
cur = nodes.hd->next;
while(cur->node != NULL) {
debug("%d ", *(int*)cur->node->key);
cur = cur->next;
} debug("\r\n----\r\n"); debug("\r\n"); debug("[Testing list insertion sort].. done. \r\n"); END:
while(1);
return 0;
} #endif /* LIST_INSERTION_DEBUG */
#ifndef __LIST_INSERTION_H__
#define __LIST_INSERTION_H__ #define LIST_INSERTION_DEBUG typedef int(*cmp_func)(void*, void*);
typedef void (*dbg_func)(void*, int, int); struct s_node{
void* key; void *data;
}; struct s_clist{
struct s_clist *pre;
struct s_clist *next;
struct s_node *node;
}; struct s_nodes
{
struct s_clist *hd;
int nodes_cnt;
int max_nodes_cnt; struct s_clist *cur;
}; int init_list(struct s_clist *hd, int max_cnt, struct s_nodes *nodes); int insert_list_sort(struct s_node *node, struct s_nodes *nodes); int is_list_full(struct s_nodes *nodes); int get_list_cnt(struct s_nodes *nodes); int delete_item_from_list(struct s_node *node, struct s_nodes *nodes); #endif /* __LIST_INSERTION_H__ */
#pragma once

#include <windows.h>
#ifdef _WIN32
#define msleep(x) Sleep(x)
#endif #include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h> #define MY_DEBUG
#ifdef MY_DEBUG
#define debug printf
#else
#define debug(x,argc, __VA_ARGS__) ;
#endif /* MY_DEBUG */ #define F_S() debug("[%s]..\r\n", __FUNCTION__)
#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. Struts学习之类型转换

    * 从页面中获取对应的内容        * 在动作类action中,声明与页面中表单name属性的值同名的属性        * 提供get和set方法        * struts2框架就会通过 ...

  2. Java 注解机制

    一.注解中的信息已经在Class中了,我们应该如何读取出来 java.lang.reflect.AnnotatedElement接口: public Annotation[] getAnnotatio ...

  3. Redis Sentinel的Redis集群(主从&Sharding)高可用方案

    在不使用redis3.0之后版本的情况下,对于redis服务端一般是采用Sentinel哨兵模式,也就是一主多备的方式. 这里,先抛出三个问题, 问题1:单节点宕机数据丢失?问题2:多节点(节点间没有 ...

  4. nodejs:注册登录session出错以及连接Mongodb数据库时Error connecting to database解决方案

    (1)nodejs:注册登录session出错 解决办法: 在app.js 中将var MongoStore =  require(connect-mongo')改为var MongoStore =  ...

  5. Core第三方开源Web框架

    NET Core第三方开源Web框架YOYOFx   YOYOFx框架 YOYOFx是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台. 本着学习的态度,造了这个 ...

  6. SQL Server 内存不足引起的并发症

    第一:cpu 1.内存不足就会有频繁的页面调入调出.这个过程是要有cpu的参与的.所以这个要影响cpu! 2.内存不足可能会引有用起执行计划被清除.当起次要执行时.这个就要重编译一次!

  7. 如何使用沉浸式状态栏,让你的app风格更好看

    大家都知道,传统的手机状态栏非黑即白,经常让整个app显得不是那么的好看,如何让状态栏的颜色跟你整个界面的颜色能够融为一体,这是我们一直想要的,现在给大家展示一下: 由图可见,第一张是没有使用沉浸式状 ...

  8. selenium的config.ini

    config.ini # What WebDriver to use for the tests#driver=phantomjs#driver=firefoxdriver=chrome#driver ...

  9. cocos2dx CCTextFieldTTF

    CCTextFieldTTF是一个提供文本输入的控件. 先上个简单的例子 CCSize size = __winSize; CCTextFieldTTF* textEdit = CCTextField ...

  10. Java图形化界面设计——布局管理器之null布局(空布局)

    一般容器都有默认布局方式,但是有时候需要精确指定各个组建的大小和位置,就需要用到空布局. 操作方法: 1)       首先利用setLayout(null)语句将容器的布局设置为null布局(空布局 ...