#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()函数的实现的更多相关文章

  1. C语言qsort()函数的使用

    C语言qsort()函数的使用 qsort()函数是 C 库中实现的快速排序算法,包含在 stdlib.h 头文件中,其时间复杂度为 O(nlogn).函数原型如下: void qsort(void ...

  2. C语言qsort函数用法

    qsort函数简介 排序方法有很多种:选择排序,冒泡排序,归并排序,快速排序等. 看名字都知道快速排序是目前公认的一种比较好的排序算法.因为他速度很快,所以系统也在库里实现这个算法,便于我们的使用. ...

  3. CGO封装C语言qsort函数

    封装qsort函数 package qsort /* #include <stdlib.h> typedef int (*qsort_cmp_func_t) (const void* a, ...

  4. C语言qsort函数算法性能测试

    对于该算法的复杂性.一个直接的方法是测量的一定量的算法级数据的执行时间的感知. 随着C语言提供qsort对于示例.随着100一万次的数据,以测试其计算量.感知O(nlg(n))时间成本: C码如下面: ...

  5. qsort函数、sort函数【转】

    http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...

  6. qsort函数、sort函数 (精心整理篇)

    先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...

  7. 使用C语言中qsort()函数对浮点型数组无法成功排序的问题

    一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...

  8. qsort函数、sort函数

    先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...

  9. c语言中qsort函数的使用、编程中的一些错误

    qsort()函数: 功能:相当于c++sort,具有快排的功能,复杂度的话nlog(n)注:C中的qsort()采用的是快排算法,C++的sort()则是改进的快排算法.两者的时间复杂度都是nlog ...

随机推荐

  1. 实现QQ内打开链接跳转至浏览器

    经常遇到域名拦截的问题,不管是QQ还是微信在移动端如果打开被拦截是件很麻烦的事情. 那么,你怎样才能有效地避免这个问题呢?很多站长说域名可以抵御拦截?但是你有没有想过域名拦截的机制是什么? <? ...

  2. pynlpir.License过期问题解决方案

    报错信息:pynlpir.LicenseError: Your license appears to have expired. Try running "pynlpir update&qu ...

  3. 14.在Python中lambda函数是什么

    在Python中lambda函数是什么? It is a single expression anoymous function often used as inline function. lamb ...

  4. mac OS nvm 常用命令

    nvm install stable ## 安装最新稳定版 node,当前是node v10.15.0 (npm v6.4.1) nvm install <version> ## 安装指定 ...

  5. 限流 - Guava RateLimiter

    2019独角兽企业重金招聘Python工程师标准>>> 限流 限流的目的是通过对并发访问/请求进行限速或者一个时间窗口内的的请求进行限速来保护系统,一旦并发访问/请求达到限制速率或者 ...

  6. C++编程入门--No.6

    题目:用*号输出字母C的图案. 程序分析:可先用'*'号在纸上写出字母C,再分行输出. #include <bits/stdc++.h> using namespace std; int ...

  7. 图论--最小生成树--Prim算法(带边输出)模板

    #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 100 ...

  8. OSG程序设计之Hello World 3.0

    直接上代码: #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgViewer/View ...

  9. hdu1074之状压dp

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; cons ...

  10. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...