qsort
qsort包含在头文件#include<stdlib.h>中,函数原型如下:
void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
其中
void* base 是指向需要被排序数组的第一个地址的指针
size_t num 是base指针指向的数组中包含元素的个数,即数组的大小
comparator 是一个指向函数的指针,函数返回类型为int,函数有两个void*类型的参数,在使用中需要向下转为具体类型的指针,例如
int compare(const void* a,const void* b){
return *(int*)a-*(int*)b;
}
如果需要比较两个int类型的元素,需要将void*指针转为int*,*(int*)代表数组中某个整数值,如果认为第一个参数a需要比第二个参数b大,返回正值,否则返回负值;升序排序需要用第一个参数减去第二个参数,降序排序需要用第二个参数减去第一个参数
使用例子如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct product{
char name[16];
float price;
};
int array_int[5]={4,1,2,5,3};
char array_char[5]={'a','c','b','e','d'};
double array_double[5]={1.2,2.3,5.2,4.6,3.5};
//整数比较函数
int compare_int(const void* a,const void* b){
return *(int*)a-*(int*)b;
}
//字符比较函数
int compare_char(const void* a,const void* b){
return *(char*)a-*(char*)b;
}
//浮点比较函数
int compare_double(const void* a,const void* b){
return *(double*)a>*(double*)b?1:-1;
}
//结构比较函数(按照结构中的浮点数值排序)
int compare_struct_float(const void*a,const void*b){
struct product* ia=(struct product*)a;
struct product* ib=(struct product*)b;
return ia->price>ib->price?1:-1;
}
//结构比较函数(按照结构中的字符串排序)
int compare_struct_str(const void*a,const void*b){
struct product* ia=(struct product*)a;
struct product* ib=(struct product*)b;
return strcmp(ia->name,ib->name);
}
//打印函数
void print_int(const int* a,int length){
printf("升序排序后的int数组:\n");
for(int i=0; i<length-1; i++)
printf("%d ",a[i]);
printf("%d\n",a[length-1]);
}
void print_char(const char* a,int length){
printf("升序排序后的char数组:\n");
for(int i=0; i<length-1; i++)
printf("%c ",a[i]);
printf("%c\n",a[length-1]);
}
void print_double(const double* a,int length){
printf("升序排序后的double数组:\n");
for(int i=0; i<length-1; i++)
printf("%.2f ",a[i]);
printf("%.2f\n",a[length-1]);
}
void print_struct_array(struct product *array, int length)
{
for(int i=0; i<length; i++)
printf("[ name: %s \t price: $%.2f ]\n", array[i].name, array[i].price);
puts("--");
}
void main()
{
struct product structs[] = {{"mp3 player", 299.0f}, {"plasma tv", 2200.0f},
{"notebook", 1300.0f}, {"smartphone", 499.99f},
{"dvd player", 150.0f}, {"matches", 0.2f }};
//整数排序
qsort(array_int,5,sizeof(array_int[0]),compare_int);
print_int(array_int,5);
//字符排序
qsort(array_char,5,sizeof(array_char[0]),compare_char);
print_char(array_char,5);
//浮点排序
qsort(array_double,5,sizeof(array_double[0]),compare_double);
print_double(array_double,5);
//结构中浮点排序
int len = sizeof(structs)/sizeof(struct product);
qsort(structs,len,sizeof(struct product),compare_struct_float);
printf("按结构中float升序排序后的sturct数组:\n");
print_struct_array(structs, len);
//结构中字符串排序
qsort(structs,len,sizeof(struct product),compare_struct_str);
printf("按结构中字符串升序排序后的sturct数组:\n");
print_struct_array(structs, len);
}
参考:
http://apps.hi.baidu.com/share/detail/2056555.
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
http://www.anyexample.com/programming/c/qsort__sorting_array_of_strings__integers_and_structs.xml
http://www.cnblogs.com/yeye518/archive/2011/10/07/2231607.html