C语言qsort()函数的实现
#include <stdio.h>
void qsort(void * base, int num, int width, int (*comp)(const void *, const void *));
void sort(char * lo, char * hi, int (*comp)(const void *, const void *), int width);
char * partition(char * lo, char * hi, int (*comp)(const void *, const void *), int width);
void swap(char * a, char * b, int width);
int mycomp(const void * p1, const void * p2); int main(void) {
int i;
int a[10] = {5, 3, 2, 9, 12, 6, 7, 10, 11, 1};
qsort(a, 10, 4, mycomp);
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n"); return 0;
} int mycomp(const void * p1, const void * p2) {
const int * a1 = (const int *) p1;
const int * a2 = (const int *) p2;
if (*a1 < *a2)
return -1;
else if (*a1 == *a2)
return 0;
else
return 1;
} void qsort(void * base, int num, int width, int (*comp)(const void *, const void *)) {
char * lo = (char *) base;
char * hi = (char *) base;
hi += (num - 1) * width;
sort(lo, hi, comp, width);
} void sort(char * lo, char * hi, int (*comp)(const void *, const void *), int width) {
char * p;
if (lo >= hi) return; p = partition(lo, hi, comp, width);
sort(lo, p - width, comp, width);
sort(p + width, hi, comp, width);
} char * partition(char * lo, char * hi, int (*comp)(const void *, const void *), int width) {
char * i = lo;
char * j = hi; while (i <= j) {
do {
i += width;
if (i == hi) break;
} while ((*comp)(i, lo) < 0); while (1) {
if ((*comp)(j, lo) <= 0)
break;
j -= width;
} if (i <= j)
swap(i, j, width);
}
swap(j, lo, width);
return j;
} //swap one byte at a time
void swap(char * a, char * b, int width) {
char tmp;
while (width--) {
tmp = *a;
*a++ = *b;
*b++ = tmp;
}
}
C语言qsort()函数的实现的更多相关文章
- C语言qsort()函数的使用
C语言qsort()函数的使用 qsort()函数是 C 库中实现的快速排序算法,包含在 stdlib.h 头文件中,其时间复杂度为 O(nlogn).函数原型如下: void qsort(void ...
- C语言qsort函数用法
qsort函数简介 排序方法有很多种:选择排序,冒泡排序,归并排序,快速排序等. 看名字都知道快速排序是目前公认的一种比较好的排序算法.因为他速度很快,所以系统也在库里实现这个算法,便于我们的使用. ...
- CGO封装C语言qsort函数
封装qsort函数 package qsort /* #include <stdlib.h> typedef int (*qsort_cmp_func_t) (const void* a, ...
- C语言qsort函数算法性能测试
对于该算法的复杂性.一个直接的方法是测量的一定量的算法级数据的执行时间的感知. 随着C语言提供qsort对于示例.随着100一万次的数据,以测试其计算量.感知O(nlg(n))时间成本: C码如下面: ...
- qsort函数、sort函数【转】
http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...
- qsort函数、sort函数 (精心整理篇)
先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...
- 使用C语言中qsort()函数对浮点型数组无法成功排序的问题
一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...
- qsort函数、sort函数
先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...
- c语言中qsort函数的使用、编程中的一些错误
qsort()函数: 功能:相当于c++sort,具有快排的功能,复杂度的话nlog(n)注:C中的qsort()采用的是快排算法,C++的sort()则是改进的快排算法.两者的时间复杂度都是nlog ...
随机推荐
- java 之 abstract、interface
abstract (抽象) 用abstract关键字来修饰一个类时,这个类叫做抽象类: 用abstract来修饰一个方法时,该方法叫做抽象方法. 抽象方法:只有方法的声明,没有方法的实现.以分号结束: ...
- Python3 字符串格式化(%操作符)
格式符 格式符为真实值预留位置,并控制显示的格式.格式符可以包含有一个类型码,用以控制显示的类型,如下: %s 字符串 (采用str()的显示) %r 字符串 (采用repr()的显示) ...
- 《OSPF和IS-IS详解》一1.5 ARPANET内的路由选择
本节书摘来异步社区<OSPF和IS-IS详解>一书中的第1章,第1.5节,作者: [美]Jeff Doyle 译者: 孙余强 责编: 傅道坤,更多章节内容可以访问云栖社区"异步社 ...
- linux服务器(CentOS)一键安装express框架
express框架需要nodejs环境支持,没有安装node.js环境的同学可以参照下面这篇博客 linux服务器安装配置Node.js 好了,言归正传.先使用xshell或者其它软件连接我们的服务器 ...
- 数学--数论-- HDU 2601 An easy problem(约束和)
Problem Description When Teddy was a child , he was always thinking about some simple math problems ...
- POJ - 2387 Til the Cows Come Home (最短路入门)
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before ...
- 15分钟从零开始搭建支持10w+用户的生产环境(四)
上一篇文章,介绍了这个架构中,WebServer的选择,以及整个架构中扩展时的思路. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(三) 五.架构实践 前边用了三篇文章,详细介绍了这个 ...
- Java——接口相关知识
1.接口用interface来声明 //定义一个动物接口 public interface Animal{ public void eat(); public void travel(); } 2.接 ...
- ASP .NET Core 建立列表和表单View
前几篇文章对控制器Controller以及布局页_Layout相关的代码与作用介绍了一下.接下来就是建立控制器对应的列表和对应的表单. 建立Department文件夹,在文件夹下面建立普通的Index ...
- 初识CoAP协议
前言 本文介绍什么是CoAP,以及如何在物联网设备上使用它.CoAP是一种物联网协议,具有一些专门为受约束的设备而设计的有趣功能.还有其他一些可用于构建物联网解决方案的IoT协议,例如MQTT等. 物 ...