C语言中qsort快速排序使用实例
来源:才华咖 本文已影响2.07W人
来源:才华咖 本文已影响2.07W人
大家想知道在C语言中qsort是如何快速排序的吗?下面小编为大家整理了C语言中qsort快速排序使用实例,希望能帮到大家!
简单的介绍如下。
复制代码 代码如下:
/*****************************
qsort原型:
void qsort( void *base, size_t num, size_t width,
int (__cdecl *compare )(const void *elem1, const void *elem2 ) );
base:数组首地址
num: 数组元素个数
width: 每个数组元素字节数
compare:比较函数 注意类型转换
*****************************/
#include
#include //strcmp函数
#include //qsort函数
#include //fabs(double),abs(int)
int intcmp(const void*i1,const void *i2)
{
return *(int*)i1-*(int*)i2;
}
int doublecmp(const void *d1,const void *d2)
{
//return *(double*)d1 - *(double*)d2;//出现错误 double不精确的
double tmp=*(double*)d1 - *(double*)d2;
if(fabs(tmp) < 0.000001)
return 0;
else
return tmp>0 ? 1 : -1;
}
int stringcmp(const void *str1,const void *str2)
{
return strcmp(*(char**)str1,*(char**)str2);
/*
这里为什么是 *(char**)呢?比较函数的参数都是数组元素的地址。
如果是 int[],那么其元素就是int.传入的'&int[i],那么要比较的话,void *i1转换
为 int*的在取值。一样,对于字符串数组而言,char*s[]其内存放的就是各个串的首地址。
char*.所以转换为void *后。其为&(char*)。所以要从void *转换回去比较。就要用到二级指针(char**)str1,
确保str1进过一次寻址后,*str1后为char*.可参见msdn例子。
*/
}
void main()
{
printf("---------------------C中qsort使用方法(默认递增)----------------------n");
int a[]={1,2,6,8,10,7,9,40,12,6,7};
printf("-------int[]数组qsort测试-------nbefore sort:n");
for(int i=0;i!=sizeof(a)/sizeof(int);i++)
printf("%d ",a[i]);
qsort(a,sizeof(a)/sizeof(int),sizeof(a[0]),intcmp);
printf("nafter sort:n");
for(int i=0;i!=sizeof(a)/sizeof(int);i++)
printf("%d ",a[i]);
printf("n");
printf("-------double[]数组qsort测试-------nbefore sort:n");
double d[]={1.12,1.1236,1.36,1.2456,2.48,2.24123,-2.3,0};
for(int i=0;i!=sizeof(d)/sizeof(double);i++)
printf("%f ",d[i]);
qsort(d,sizeof(d)/sizeof(double),sizeof(d[0]),doublecmp);
printf("nafter sort:n");
for(int i=0;i!=sizeof(d)/sizeof(double);i++)
printf("%f ",d[i]);
printf("n");
printf("-------string: char*[]数组qsort测试-------nbefore sort:n");
char *str[]={"hello","hi","you","are","baby"};
for(int i=0;i!=sizeof(str)/sizeof(str[0]);i++)
printf("%s ",str[i]);
printf("nafter sort:n");
qsort(str,sizeof(str)/sizeof(str[0]),sizeof(str[0]),stringcmp);
for(int i=0;i!=sizeof(str)/sizeof(str[0]);i++)
printf("%s ",str[i]);
printf("n");
}
C++中const-cast与reinterpret-cast运算符的用法
Cisco交换机用QOS技术实现限速
C语言中字符串常用函数strcat与strcpy的用法
深入理解JS实现快速排序和去重javascript技巧
快速入睡的方法The Ways to Get Sleep Quickly高中英语作文
JavaScript fontcolor方法入门实例
C语言中Static和Const关键字的作用
C语言中const关键字的使用方法
Javascript实例教程如何使用HoTMetal
MicrosoftProject中显示语言中文设置为英语的方法
DOS的Interlnk、Intersvr、Qbasic命令使用说明
PHP中CURL的CURLOPT-POSTFIELDS参数使用细节
MySQL安装时出现current root password的解决方法
C语言中strstr()函数的使用分析
总结C语言中const关键字的使用
口才快速训练安排方法
如何使用Fireworks快速给人像调色
CSS中的Box Model盒属性的使用实例
DOS的Prompt、Restore命令使用说明
PHOTOSHOP中关于Ctrl的常用快捷键
BCS电流方程(BCScurrentequation)中学生物理知识大全精选
C语言程序设计实习心得
C语言socket编程使用方法
PHP中使用cURL实现Get和Post请求的方法
JavaScript中Cookie操作实际案例
Protecting water resources英语作文
C语言关键字const的使用
关于javascript中encodeURI和decodeURI的使用方法
有关使用 Aspose.Words for .NET来动态的生成word文档中的数据表
PHOTOSHOP中关于Ctrl常用的快捷键
Unit 8 Sports Stars的教学实录及反思案例
英语阅读:VIT declines to acquire Ang Mo Kio property
AssistMyFathertoDoExercises初中英语作文
c语言程序设计心得(多篇)
JavaScript中创建字典对象(dictionary)的实例
中考英语阅读理解训练John sent for a doctor
JavaScript快速排序实现实例教程
C语言socket编程使用方法简介
Swift与C语言指针结合使用实例
英语作文:Crossing The River to School