c/c++中排序的使用之—qsort

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章