[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に強制的に変換し、それを前述の*要素位置ポインターポイントの1つに追加して取得します。

整数

#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 ++

一般に、配列を並べ替える場合、これは簡単です。3番目のパラメーターの「比較」関数をデフォルトにすることができ、デフォルトは昇順であるためです。

プリミティブ

3つのパラメータがあります:
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 21
降順

ライブラリ関数比較関数(名前空間標準)

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;
}

出力:edcba

ソート構造-カスタム比較機能

#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