02_选择排序

#include 
#include 
using namespace std;

//每一趟都从所有元素当中选择出最小元素排序 非相邻元素比较
template
void SelectionSort(vector &a)  //O(N^2)
{
	int i = 0;
	int j = 0;

	for (i = 0; i< a.size(); i++)
	{
		for(j = i+1; j < a.size(); j++)
		{
			if (a[i] > a[j])
			{
				Comp tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}	
	}
}

void main()
{
	vector a;
	a.push_back(8);
	a.push_back(3);
	a.push_back(6);
	a.push_back(1);

	cout << "排序前:";
	for(int i = 0; i < a.size(); i ++)
		cout << a[i] << " ";
	cout << endl;

	SelectionSort(a);
	cout << "排序后:";
	for(int i = 0; i < a.size(); i ++)
		cout <<  a[i] << " ";
	cout << endl;

	system("pause");
}

你可能感兴趣的:(排序算法)