[C/C++]快速排序库函数

C语言

原函数

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

头文件

#include<stdlib.h>

代码示例

说明

其中 const void * a表示声明了一个常量指针 a,(int*)a指的是将常数指针 a强制转化为整型指针 a,那么再与前面加一个*就表示取这个指针所指向位置的元素了。

整型

#include<iostream>
#include<stdlib.h>
using namespace std;

int a[] = { 4, 3, 2, 1 };

int compare(const void* a, const void* b);

int main()
{
	int i;
	qsort(a, 4, sizeof(int), compare);
	for ( i = 0; i < 4; i++)
	{
		cout << a[i] << endl;
	}
	
	return 0;
}

int compare(const void* a, const void* b)//升序
{
	return *(int*)a - *(int*)b;	
}

输出:1 2 3 4

字符型

#include<iostream>
#include<stdlib.h>
using namespace std;

char a[][4] = { {"bca"}, {"abc"}, {"cab"} };

int compare(const void* a, const void* b);

int main()
{
	int i;
	qsort(a, 3, sizeof(a[0]), compare);
	for ( i = 0; i < 3; i++)
	{
		cout << a[i] << endl;
	}
	return 0;
}

int compare(const void* a, const void* b)//升序
{
	return strcmp((char*)a, (char*)b);
	//return *(int*)a - *(int*)b;	
}

输出:abc bca cab

C++

一般对数组排序时,这个简单,因为第三个参数"compare"函数可缺省,默认为升序排列。

原函数

有三个参数:
1、数组的起始地址;
2、数组的结束地址;
3、如何排序(从大到小或从小到大)。

头文件

#include<algorithm>

代码示例

比较函数缺省

#include<iostream>
#include<algorithm>
using namespace std;

int value[] = { 5, 4, 3, 2, 1 };

bool compare(int a, int b);

int main()
{
	int i;
	sort(value, value + 5);
	for ( i = 0; i < 5; i++)
	{
		cout << value[i] << endl;
	}
	return 0;
}

输出:1 2 3 4 5

部分数组排序

你还可以这么玩:

#include<iostream>
#include<algorithm>
using namespace std;

int value[] = { 5, 4, 3, 2, 1 };

bool compare(int a, int b);

int main()
{
	int i;
	sort(value + 1, value + 5);
	for ( i = 0; i < 5; i++)
	{
		cout << value[i] << endl;
	}
	return 0;
}

输出:5 1 2 3 4

自定义比较函数

#include<iostream>
#include<algorithm>
using namespace std;

int value[] = { 5, 4, 3, 2, 1 };

bool compare(int a, int b);

int main()
{
	int i;
	sort(value, value + 5, compare);
	for ( i = 0; i < 5; i++)
	{
		cout << value[i] << endl;
	}
	return 0;
}

bool compare(int a, int b) //降序
{
	return a > b;
}

输出:5 4 3 2 1
降序排列

库函数比较函数(命名空间std)

less<数据类型>()
greater<数据类型>()

字符型:

#include<iostream>
#include<algorithm>
using namespace std;

char value[] = { 'a', 'b', 'c', 'd', 'e' };

int main()
{
	int i;
	sort(value, value + 5, greater<char>());
	for ( i = 0; i < 5; i++)
	{
		cout << value[i] << endl;
	}
	return 0;
}

输出:e d c b a

对结构体排序-自定义比较函数

#include<iostream>
#include<algorithm>
using namespace std;

struct student
{
	string name;
	int age;
}value[3]; 

bool compare(const student& a, const student& b);

int main()
{
	int i;
	value[0] = { "Kite", 10 };
	value[1] = { "Tom", 15 };
	value[2] = { "Mike", 20 };
	sort(value, value + 3, compare);
	for ( i = 0; i < 3; i++)
	{
		cout << value[i].name << " " << value[i].age << endl;
	}
	return 0;
}

bool compare(const student& a, const student& b)
{
	if (a.name == b.name)
	{
		return a.age < b.age;
	}
	else
	{
		return a.name < b.name;
	}
}

输出:
Kite 10
Mike 20
Tom 15

对结构体排序-重载关系运算符

#include<iostream>
#include<algorithm>
using namespace std;

struct student
{
	string name;
	int age;
	bool operator < (const student& b) const //符号重载
	{
		if (name == b.name)
		{
			return age < b.age;
		}
		else
		{
			return name < b.name;
		}
	}
	//简单地说,和之前的 compare 函数相比,就是把当前“对象”看作 a 了
}value[3]; 

int main()
{
	int i;
	value[0] = { "Kite", 10 };
	value[1] = { "Tom", 15 };
	value[2] = { "Mike", 20 };
	sort(value, value + 3);
	for ( i = 0; i < 3; i++)
	{
		cout << value[i].name << " " << value[i].age << endl;
	}
	return 0;
}

输出:
Kite 10
Mike 20
Tom 15

猜你喜欢

转载自blog.csdn.net/weixin_44092088/article/details/109900406