qsort的用法

/************************************************************************/
/* 
author:minglu
data: 2014/4/11
purpose:利用库函数qsort来对各种类型数据进行排序。
注意:void qsort(void* base, size_t num, size_t width, int (*)compare(const void* elem1, const void* elem2)),
其中的base为待排序数组始发地址,一般为数组名
num 为参加排序元素个数
width为元素字节数,为sizeof(ElemType)
compare为比较函数,得自己写。
elem1和elem2为指向元素的指针。明白这点尤为重要!!
*/
/************************************************************************/
#include<iostream>
#include<cstring>
using namespace std;
//利用qsort对int,float,char,short等类型的数组排序,此处以int类型为例
int compareInt(const void* p, const void *q)
{
	return *(int*)p - *(int*)q;
}

int compareChar(const void* p, const void *q)
{
	return *(char*)p - *(char*)q;
}

//对字符串进行排序,方法一
int compareStr(const void* p, const void *q)
{//由于对二维数组char a[10][10]进行排序,故p,q为指向char[10]的类型变量,即char(*)[10]类型,数组指针类型
//如p = & a[2];则*p为a[2]
//	return strcmp((char*)p, (char*)q); //可以
	return strcmp(*(char(*)[10])p, *(char(*)[10])q);

}
//对字符串进行排序,方法二
int comparePStr(const void* p, const void* q)
{//由于是对char*数组中的元素进行排序,故p,q为指向char*的类型变量,即char**类型
	return strcmp(*(char**)p, *(char**)q); //可以
}

template<class ElemType>
void printArr(ElemType arr[],int length)
{
	int i = 0;
	for (i=0; i<length; ++i)
		cout << arr[i] << " ";
	cout << endl;
}

void test1()
{
	cout << "对整型数组进行排序" <<endl;
	int a[5] = {0, 3, 2, 1, 4};
	qsort(a, sizeof(a)/sizeof(int), sizeof(int), compareInt);
	printArr(a,sizeof(a)/sizeof(int));

}

void test2()
{
	cout << "对字符数组进行排序" << endl;
	char a[5] = {'i', 'l', 'o', 'v', 'e'};
	qsort(a, sizeof(a)/sizeof(char), sizeof(char), compareChar);
	printArr(a,sizeof(a)/sizeof(char));
}

void test3()
{
	cout << "对字符串进行排序" << endl;
	//在C语言中字符串常量的本质表示其实是一个地址
	char* p[10] = {"i", "love", "you", "with", "all", "my", "heart"};
	char a[10][10] = {"i", "love", "you", "with", "all", "my", "heart"};
	qsort(p, 7, sizeof(char*), comparePStr );
	qsort(a, 7, sizeof(a[0]), compareStr );
	for (int i = 0; i < 10; ++i)
		if (p[i])
			cout << p[i] << " ";
	cout << endl;
	for (int i = 0; i < 7; ++i)
			cout << a[i] << " ";
	cout << endl;
}


//按结构体中某个关键字排序
struct tagNode
{
	double data;
	int other;
};
int compareStruct(const void* p, const void* q)
{
	struct tagNode* ptr1 = (struct tagNode*)p;
	struct tagNode* ptr2 = (struct tagNode*)q;
	if (ptr1->data > ptr2->data)
		return 1;
	else if (ptr1->data < ptr2->data)
		return -1;
	else
		return 0;
}

void test4()
{
	cout << "按结构体中某个关键字排序" << endl;
	struct tagNode s[10] = {1.1, 1, 3.3, 3, 2.2, 2, 4.4, 4, 6.6, 6, 5.5, 5,  7.7, 7};
	qsort(s, 7, sizeof(struct tagNode), compareStruct);
	for (int i=0; i<7;++i)
		cout << s[i].data <<" " << s[i].other << "  ";
	cout << endl;
}
int main()
{
	char *a [ ] = {"China","French","America","German"};
	char **s  =  a;
	cout << s << endl;
	test1();
	test2();
	test3();
	test4();
	return 0;
}//end main

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值