现在的位置: 首页 > 综合 > 正文

时间复杂读为n的排序

2017年11月15日 ⁄ 综合 ⁄ 共 504字 ⁄ 字号 评论关闭

排序年龄的算法

//复杂度为On的排序算法
#include<iostream>
#include <vector>
using namespace std;
const int oldestage=99;

void OnSort(vector<int> &v1)
{
	if(v1.size()==0)return;
	vector<int> v2(oldestage+1,0);
	for (auto i =0;i<v1.size();i++)
	{
		int m =v1[i];
		if (m<0||m>oldestage)
			throw new std::exception("age out of range!!");
		v2[m]++;
	}
	int temp=0;
	for (auto i =0;i<oldestage+1;i++)
	{
		for (auto j =0;j<v2[i];j++)
		{
			cout<<i<<" ";
			temp++;
		}
	}
	cout<<temp<<" ";
}

void main()
{
	vector<int> v;
	v.reserve(1000);
	for (auto i =0;i<1000;i++)
		v.push_back((rand()%90+10));
	OnSort(v);
	system("pause");
}

抱歉!评论已关闭.