/* Shell Sorting.
* Implemention history:.
* 2013-09-15, Mars Fu, first version.
*/ /* [Shell Sorting Algorithm].
* Published by Donald L. Shell [CACM 2, 7(July 1959), 30-32].
*/ #include "stdafx.h" #include "shellsort.h"
#include <math.h> static int
get_increment(int s)
{
int next_hs; next_hs = (1 << s); return next_hs;
} int
do_shell_sort(void* src, int src_sz, int n, cmp_func cmp, exc_func exchange, dbg_func dbg)
{
int t;
int h;
int i,j;
float n2; F_S(); if (src == NULL || cmp == NULL || exchange == NULL) return 0;
if (src_sz <= 0 || n <= 0) return 0; /* get @t for aux table {h(t),...,h(0)}
*/
n2 = 2;
t = log((double)n) / log((double)n2) + (n%2 ? 1 : -1); /* loop aux table for shell sorting
*/
while (t >= 0) { h = get_increment(t); /* get h(t) */ /* loop all parts of @src, each part has @h items to be compared.
*/
for (i = 0; i < n/h; ++i) { /* do the comparison and exchanging for each part.
*/
for ( j = i+h; j < n; j += h) {
if (cmp((char*)src + (j-h)*src_sz, (char*)src + j*src_sz) > 0) {
exchange((char*)src + (j-h) * src_sz, (char*)src + j * src_sz);
}
}
} --t; if (dbg != NULL) dbg(src, n, h);
} F_E(); return 1;
} #ifdef SHELL_SORT_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
exchange_int_item(void* lv, void* rv)
{
int tmp_lv, tmp_rv; tmp_lv = *(int*)lv;
tmp_rv = *(int*)rv; *(int*)lv = *(int*)rv;
*(int*)rv = tmp_lv;
} 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; debug("[Testing shell sort].. \r\n"); 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"); ret = do_shell_sort((void*)int_items, sizeof(int), cnt,
int_increasing_cmp, exchange_int_item, debug_func);
if (!ret) {
debug("failed. \r\n");
goto END;
} debug("dst database:\r\n----\r\n");
for (i = 0; i < cnt; ++i) {
debug("%d ", int_items[i]);
}
debug("\r\n----\r\n"); debug("\r\n"); debug("[Testing shell sort].. done. \r\n"); END:
while(1);
return 0;
} #endif /* SHELL_SORT_DEBUG */
#ifndef __SHELLSORT_H__
#define __SHELLSORT_H__ #define SHELL_SORT_DEBUG typedef int(*cmp_func)(void*, void*);
typedef void (*exc_func)(void*, void*);
typedef void (*dbg_func)(void*, int, int); int do_shell_sort(void* src, int src_sz, int n, cmp_func cmp, exc_func exchange, dbg_func dbg); #endif /* __SHELLSORT_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__)

Mars

Sep 15th, 2013

Foundation Sorting: Shellsort的更多相关文章

  1. Foundation Sorting: Quicksort

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

  2. Foundation Sorting: Single List Insertion Sort

    /* List Insertion Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ #incl ...

  3. 小白科普之JavaScript的数组

    一.与其他语言数据的比较    相同点:有序列表    不同点:js的数组的每一项可以保存任何类型的数据:数组的大小是可以动态调整的 二.数组创建的两种方法 1)  var colors = new ...

  4. JavaScript版排序算法

    JavaScript版排序算法:冒泡排序.快速排序.插入排序.希尔排序(小数据时,希尔排序会比快排快哦) //排序算法 window.onload = function(){ var array = ...

  5. js排序的方法

    //排序算法 window.onload = function(){     var array = [0,1,2,44,4,                 324,5,65,6,6,        ...

  6. 20172309 《Java软件结构与数据结构》实验三报告

    课程:<程序设计与数据结构(下)> 班级:1723 姓名: 王志伟 学号:20172309 实验教师:王志强老师 实验日期:2018年11月2日 必修/选修: 必修 实验内容: 实验一: ...

  7. iOS Foundation 框架概述文档:常量、数据类型、框架、函数、公布声明

    iOS Foundation 框架概述文档:常量.数据类型.框架.函数.公布声明 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业 ...

  8. Algorithm in Practice - Sorting and Searching

    Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisi ...

  9. Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver

    ========================== Foundation框架下的常用类 ========================== 一.[NSNumber] [注]像int.float.c ...

随机推荐

  1. BZOJ 4321: queue2( dp )

    dp(i, j, 1)表示前i个, 有j对是不合法的, i和i-1是在一起的. dp(i, j, 0)表示前i个, 有j对是不合法的, i和i-1不在一起的. 转移我们只需考虑是多了一对不合法的还是少 ...

  2. jQ中prop与attr的区别

    1.prop适用于HTML元素本身就带有的固有属性 2.attr适用于HTML元素我们自定义的属性 <input type="checkbox" value="复选 ...

  3. 测试scanf输入含非法控制符

    心得: 学到scanf命令时第一个想到的就是可以利用scanf做一个十进制转16进制.八进制的小程序,很天真的以为也可以转二进制,在搜索字符控制符的时候才知道原来没有二进制的控制字符,需要换算出来得出 ...

  4. python进阶1--数据库支持

    数据库支持 1.连接和游标 1)connect函数,该函数有多个参数,而具体使用那个参数取决于数据库.--连接数据库 常用参数: dsn:数据源名称 user:用户名 password:用户密码 ho ...

  5. 五年26个版本:Linux系统内核全程回顾

    Phoronix.com今天将他们对Linux系统的研究发挥到了极致:从2005年年中的2.6.12,到正在开发中的2.6.37,五年多来的26个Linux内核版本来了个“群英荟萃”! 完成如此庞大规 ...

  6. HBase ElasticSearch

    http://www.open-open.com/doc/view/c2af706064f84e128d0f74826523a1ea http://www.open-open.com/doc/view ...

  7. [置顶] What is the difference between Category and Class Extension?

    细心的人会发现当我们new 一个文件的时候会发现下图的部分. 但是这个问题来了Category 和 Extension 就近又什么区别呢? 1:什么是Category? 实现这样一种场景,当我们用我们 ...

  8. TCP/IP笔记 三.运输层(2)——TCP 流量控制与拥塞控制

    TCP 的流量控制与拥塞控制可以说是一体的.流量控制是通过滑动窗口实现的,拥塞避免主要包含以下2个内容: (1)慢开始,拥塞避免 (2)快重传,快恢复 1.流量控制——滑动窗口 TCP采用大小可变的滑 ...

  9. oracle slient静默安装并配置数据库及仅安装数据库不配置数据库shell

    <1,>仅安装数据库软件不配置数据库 ./x86oracle.sh /ruiy/ocr/DBSoftware/app/oracle /ruiy/ocr/DBSoftware/app/ora ...

  10. Google市场推广统计

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