Foundation Sorting: Quicksort
/* Quick Sorting.
* Implementation history:.
* 2013-09-15, Mars Fu, first version.
*/ /* [Quicksort Algorithm]
* Published by C.A.R.Hoare,[Comp. J.5(1962),10-15].
*/ #include "stdafx.h" #include "quicksort.h" int
do_quick_sort(void* src, int src_sz, int n, cmp_func cmp, exc_func exchange, dbg_func dbg)
{
const int M = 0;
int i,j,k;
int l,r;
char *kv, *ki, *kj;
static int h = 0; F_S(); if (src == NULL || cmp == NULL || exchange == NULL) return 0;
if (src_sz <= 0 || n <= 0) return 0; Q1:
/* initialize
*/
if (n <= M) {
/* do straight insertion sort
*/
return 1;
}
else {
l = 0;
r = n-1;
} Q2:
/* begine new stage
*/
i = l;
j = r + 1;
kv = (char*)src + l * src_sz; Q3:
do {
/* compare @kv with @ki
*/
do {
++i;
if (i >= j) break;
ki = (char*)src + i * src_sz;
} while (cmp(ki, kv) < 0); /* compare @kv with @kj
*/
do {
--j;
if (j <= i) break;
kj = (char*)src + j * src_sz;
} while (cmp(kv, kj) < 0); /* test i:j
*/
if (j <= i) { /* found the absolute index @j for @kv,do exchanging here and break.
*/
if (j < i) {
kj = (char*)src + j * src_sz;
exchange(kv, kj);
}
else {
/* @i == @j means no need to exchange,having been sorted already.
*/
} if (dbg) dbg(src, n, ++h);
break;
}
else {
ki = (char*)src + i * src_sz;
kj = (char*)src + j * src_sz;
exchange(ki, kj); if (dbg) dbg(src, n, ++h);
}
} while (1); Q4:
/* do the same procedure via recursion with the changed @left or @right.
*/
if(i == j) {
/* the first item is sorted already, move forward 1 item, sort again.
*/
do_quick_sort((char*)src + 1*src_sz, src_sz, n-1, cmp, exchange, dbg);
}
else {
if (j-l > 1) {
do_quick_sort((char*)src + l*src_sz, src_sz, j-l, cmp, exchange, dbg);
} if (r-j > 1) {
do_quick_sort((char*)src + i*src_sz, src_sz, r-j, cmp, exchange, dbg);
}
} END:
F_E();
return 1;
} #ifdef QUICK_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 quick 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_quick_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 quick sort].. done. \r\n"); END:
while(1);
return 0;
} #endif /* QUICK_SORT_DEBUG */
#ifndef __QUICKSORT_H__
#define __QUICKSORT_H__ #define QUICK_SORT_DEBUG typedef int(*cmp_func)(void*, void*);
typedef void (*exc_func)(void*, void*);
typedef void (*dbg_func)(void*, int, int); int do_quick_sort(void* src, int src_sz, int n, cmp_func cmp, exc_func exchange, dbg_func dbg); #endif /* __QUICKSORT_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: Quicksort的更多相关文章
- Foundation Sorting: Shellsort
/* Shell Sorting. * Implemention history:. * 2013-09-15, Mars Fu, first version. */ /* [Shell Sortin ...
- Foundation Sorting: Single List Insertion Sort
/* List Insertion Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ #incl ...
- The algorithm learning of sort which include Bubblesort,Insertsort,Quicksort and Mergesort.
Notice : these algorithms achieved by Java. So,let's going to it. firstly, what is Bubblesort? why w ...
- Scala比较器:Ordered与Ordering
在项目中,我们常常会遇到排序(或比较)需求,比如:对一个Person类 case class Person(name: String, age: Int) { override def toStrin ...
- scala 学习笔记(02) 元组Tuple、数组Array、Map、文件读写、网页抓取示例
package yjmyzz import java.io.PrintWriter import java.util.Date import scala.io.Source object ScalaA ...
- Scala入门之Array
/** * 大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: * 1,数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储现在比较经典的是使用Hadoop,也有很多情况使用Kaf ...
- Scala集合操作
大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: 1.数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储技术现在比较经典方案是使用Hadoop,不过也很多方案采用Kafka. ...
- [LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- Array基本操作
// defined array object val arr0= ) val arr1= Array(") println(arr1()) arr1()="Hello Spark ...
随机推荐
- Servlet基础知识(四)——Servlet过滤器Filter
一.什么是过滤器: 政府大楼的安检保安,它既能对进入政府大楼的人员进行检查,只允许检查符合要求的进入:同时他也负责对出大楼的人进行检查,看他带出的东西是否符合要求. 同样的,Servlet中的过滤器既 ...
- MarkDown使用 (三)表格
MarkDown表格的用法 MarkDown表格的用法 例如: $$ \begin{array}{c|lcr} n & \text{Left} & \text{Center} & ...
- 排序算法——交换排序(冒泡排序、快速排序)(java)
一.冒泡排序 时间复杂度:O(n^2) 公认最慢的排序,每次把最大/最小的放一边,原理: [57,68,59,52] [57,68,59,52] [57,59,68,52] [57,59,52,68] ...
- php以fastCGI的方式运行在iis下,遇到的文件系统权限问题及解决方法
今天准备将一个php demo放在IIS下运行,网站在IIS下的配置是这样的: 应用程序池是集成模式下的.net framework 2.0(2.0或4.0没什么关系,因为php以fastCGI的方式 ...
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(三)GPIO操作
GPIO库的核心功能,当然就是操作GPIO了,GPIO就是"通用输入/输出"接口,比如点亮一个LED.继电器等,或者通过iic spi 1-wire等协议,读取.写入数据,这都是G ...
- (Problem 40)Champernowne's constant
An irrational decimal fraction is created by concatenating the positive integers: 0.1234567891011213 ...
- Linux中的IO复用接口简介(文件监视?)
I/O复用是Linux中的I/O模型之一.所谓I/O复用,指的是进程预先告诉内核,使得内核一旦发现进程指定的一个或多个I/O条件就绪,就通知进程进行处理,从而不会在单个I/O上导致阻塞. 在Linux ...
- Java基础—ClassLoader的理解(转)
默认的三个类加载器 Java默认是有三个ClassLoader,按层次关系从上到下依次是: Bootstrap ClassLoader Ext ClassLoader System ClassLoad ...
- ssh的学习
快毕业了.临走前帮导师搭建了gerrit,git服务器,其中涉及ssh的知识,就总结了下.希望对大家有帮助 一.前言(ssh出世的原因) 万物有因就有果,既然ssh存在,就必然有它存在的理由! 许多网 ...
- 具体解释http 协议
HTTP协议的主要特点可概括例如以下: 1.支持客户/server模式. 2.简单高速:客户向server请求服务时,仅仅需传送请求方法和路径.请求方法经常使用的有GET.HEAD.POST.每种方法 ...