Foundation Sorting: Shellsort
/* 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的更多相关文章
- Foundation Sorting: Quicksort
/* Quick Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ /* [Quicksort ...
- Foundation Sorting: Single List Insertion Sort
/* List Insertion Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ #incl ...
- 小白科普之JavaScript的数组
一.与其他语言数据的比较 相同点:有序列表 不同点:js的数组的每一项可以保存任何类型的数据:数组的大小是可以动态调整的 二.数组创建的两种方法 1) var colors = new ...
- JavaScript版排序算法
JavaScript版排序算法:冒泡排序.快速排序.插入排序.希尔排序(小数据时,希尔排序会比快排快哦) //排序算法 window.onload = function(){ var array = ...
- js排序的方法
//排序算法 window.onload = function(){ var array = [0,1,2,44,4, 324,5,65,6,6, ...
- 20172309 《Java软件结构与数据结构》实验三报告
课程:<程序设计与数据结构(下)> 班级:1723 姓名: 王志伟 学号:20172309 实验教师:王志强老师 实验日期:2018年11月2日 必修/选修: 必修 实验内容: 实验一: ...
- iOS Foundation 框架概述文档:常量、数据类型、框架、函数、公布声明
iOS Foundation 框架概述文档:常量.数据类型.框架.函数.公布声明 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业 ...
- Algorithm in Practice - Sorting and Searching
Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisi ...
- Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver
========================== Foundation框架下的常用类 ========================== 一.[NSNumber] [注]像int.float.c ...
随机推荐
- 在CentOS 7 / Gnome 3 双屏时设置主屏
在Windows中设置扩展显示器为主屏的方式非常清楚,但在Linux中就不是那么明显了,下面介绍如何完成这个设置 ------------------------------------------- ...
- 拦截API 注入进程
本文详细的介绍了在Visual Studio(以下简称VS)下实现API钩子的编程方法,阅读本文需要基础:有操作系统的基本知识(进程管理,内存管理),会在VS下编写和调试Win32应用程序和动态链接库 ...
- Netty4.0学习教程
http://blog.csdn.net/u013252773/article/details/21046697 一些属性和方法介绍 http://blog.csdn.net/zxhoo/articl ...
- WireShark抓包时TCP数据包出现may be caused by ip checksum offload
最近用WireShark抓包时发现TCP数据包有报错:IP Checksum Offload,经过查阅资料终于找到了原因 总结下来就是wireshark抓到的数据包提示Checksum错误,是因为它截 ...
- ODI学习笔记2--ODI产品架构
ODI学习笔记2--ODI产品架构 ODI产品架构: ODI提供了以下几种管理工具:Designer 用于定义数据转换逻辑,这是最常用的开发工具,大部分的开发任务,包括data store的定义,in ...
- Android中自定义属性的使用
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...
- 基于MDK的ARM-GCC开发环境建立及新唐M0的HID类设备的C++开发
一,下载安装测试arm-none-eabi-gcc编译工具链 1,查看arm-none-eabi-gcc编译工具版本 打开网页:https://sourcery.mentor.com/G ...
- 投资新兴市场和细分市场 good
新兴市场对程序员来说,就是一种新的语言.一个新的平台.一套新的框架.新兴市场因为刚刚兴起,所以几乎所有人都在同一个起跑线,特别适合后进者.我认识从一个2011年开始学习iOS开发的同学,他能能力中等, ...
- file_get_contents 超时设置
<?php $ctx = stream_context_create( array( 'http' => array( 'timeout' => 1 //设置一个超时时间,单位为秒 ...
- C功底挑战Java菜鸟入门概念干货(一)
一.认识Java 1.Java 程序比较特殊,它必须先经过编译,然后再利用解释的方式来运行. 2.Byte-codes 最大的好处是——可越平台运行,可让“一次编写,处处运行”成为可能. 3.使用 ...