数据结构------非比较类---排序

1.桶排序:

   非比较类排序

    基本思想:

    若带排序的值在 一个有限的范围内,可以设计有限个桶,待排序的值装入对应的桶,桶号就是待排序的值,顺序输出各桶的值。

 

复杂度与辅助空间:

最优时间复杂度:O(N),极限情况下每个桶只有一个数据时

 平均时间复杂度:O(N+C),C为桶内排序所花费的时间

 所需辅助空间:O(M),M为所开数组大小

#include<iostream>
using namespace std;

int main()
{
	int n;
	int a[100] = { 0 };
	int num;
	int i, j;

	cout << "shuzu daxiao" << endl;
	cin >> n;//数组大小
	cout << "输入数据" << endl;

	//桶排序
	for (i = 1; i <= n; i++)
	{
		cin >> num;
		a[num]++;//放入桶中
	}

	for (i = 0; i < 100; i++)
	{
		while (a[i] > 0)//相同的整数重复输出
		{
			cout << i << endl;//桶号即数值
			a[i]--;
		}
	}

	cout << endl;
	system("pause");
	return 0;
}

去重:

#include<iostream>
using namespace std;

int main()
{
	int n;
	int a[100] = { 0 };
	int num;
	int i, j;

	cout << "shuzu daxiao" << endl;
	cin >> n;//数组大小
	cout << "输入数据" << endl;

	//桶排序
	for (i = 1; i <= n; i++)
	{
		cin >> num;
		a[num]++;//放入桶中
	}


	//去重算法
	for (i = 0; i < 100; i++)
	{
		if (a[i])
			cout << i << endl;

	}

	cout << endl;
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39503189/article/details/81835994