欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++实现选择排序

程序员文章站 2024-03-16 15:43:10
...

源代码

#include<iostream>
#include <vector>

using namespace std;


void choose_sort(vector<int>& point)
{
	int min_num;
	int flag = 0;
	for (int i = 0; i < point.size(); i++)
	{
		min_num = point[i];
		flag = i;
		for (int j = i; j < point.size(); j++)
		{
			if (point[j] < min_num)
			{
				min_num = point[j];
				flag = j;
			}
		}
		int temp;
		temp = point[flag];
		point[flag]= point[i];
		point[i] = temp;
	}
}

int main()
{
	vector<int> point = { 9,7,5,3,4,6};
	choose_sort(point);
	for (int i = 0; i < point.size(); i++)
	{
		cout << point[i] << endl;
	}
	system("pause");
	return 0;
}