如何利用C语言中的qsort库函数实现快速排序?

        之前,我们已经写过快速排序的程序,而在C语言的库函数中就有快速排序的库函数,即为qsort, 其用法如下:


功 能: 快速排序
头文件:stdlib.h
用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
参数: 
1 待排序数组首元素的地址 
2 数组中待排序元素数量
3 各元素的占用空间大小
4 指向函数的指针,用于确定排序的顺序


       示例程序如下:
#include 
#include 

int compInc(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}

int compDec(const void *a, const void *b)
{
	return *(int *)b - *(int *)a;
}


int main()
{
	int a[5] = {11,2,13,4,7};
	int b[5] = {11,2,13,4,7};
	int len = 5;
	int i;

	printf("递增排序结果:\n");
	qsort(a, len, sizeof(a[0]), compInc);
	for(i = 0; i < len; i ++)
	{
		printf("%d ", a[i]);
	}

	printf("\n\n");


	printf("递减排序结果:\n");
	qsort(b, len, sizeof(b[0]), compDec);
		
	for(i = 0; i < len; i ++)
	{
		
		printf("%d ", b[i]);
	}

	printf("\n");

	return 0;
} 
     结果为:
递增排序结果:
2 4 7 11 13

递减排序结果:
13 11 7 4 2


      下面,继续看qsort的一些用法:
#include 
#include 
#include 
#define M 12
#define N 20


int compareInc(const void *a, const void *b)
{
	return strlen((char *)a) - strlen((char*)b);
}

int compareDec(const void *a, const void *b)
{
	return strlen((char *)b) - strlen((char*)a);
}

int main(void)
{
	int i;
	char s[M][N]=
	{
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
	};

	qsort(s, M, sizeof(char) * N, compareInc);
	for(i = 0;i < M; i++)
		printf("%s\n", s[i]);

	printf("\n");

	qsort(s, M, sizeof(char) * N, compareDec);
	for(i = 0;i < M; i++)
		printf("%s\n", s[i]);

	return 0;
}
      结果为:
May
July
June
March
April
August
October
January
December
November
February
September

September
February
November
December
October
January
August
April
March
June
July
May


#include 
#include 
#include 
#define M 12
#define N 20


int compare1(const void *a, const void *b)
{
	return *(char *)a - *(char*)b;
}

int compare2(const void *a, const void *b)
{
	return *(char *)b - *(char*)a;
}

int main(void)
{
	int i;
	char s[M][N]=
	{
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
	};

	qsort(s, M, sizeof(char) * N, compare1);
	for(i = 0;i < M; i++)
		printf("%s\n", s[i]);

	printf("\n");

	qsort(s, M, sizeof(char) * N, compare2);
	for(i = 0;i < M; i++)
		printf("%s\n", s[i]);

	return 0;
}
       结果为:
April
August
December
February
July
June
January
March
May
November
October
September

September
October
November
May
March
June
July
January
February
December
August
April


       好,欣赏最后一个qsort程序:
#include 
#include 
#define N 6

typedef struct
{
    char name[15];
    int  score;

}Student;

int compare1(const void *a,const void *b)
{
    return ((Student*)a)->score - ((Student*)b)->score;
}

int compare2(const void *a,const void *b)
{
    return *(((Student*)a)->name) - *(((Student*)b)->name);
}

void print(Student s)
{
	printf("%-15s : %d\n", s.name, s.score);
}

int main()
{
    Student s[N] =
	{
	"Zhang San", 94,
	"Li Si",     80,
	"You",       94,
	"I",        100,
	"He",        72,
	"She",       60
	};

	int i;
	qsort(s, N, sizeof(Student), compare1);
	for(i = 0; i < N; i++)
	{
		print(s[i]);
	}

	printf("\n");

	qsort(s, N, sizeof(Student), compare2);
	for(i = 0; i < N; i++)
	{
		print(s[i]);
	}

	return 0;
}
       结果为:
She             : 60
He              : 72
Li Si           : 80
You             : 94
Zhang San       : 94
I               : 100

He              : 72
I               : 100
Li Si           : 80
She             : 60
You             : 94
Zhang San       : 94


你可能感兴趣的:(S1:,C/C++,S3:,排序算法)