从当前未排序的整数中找到最小的整数,将它放在已排序的整数列表的最后。
#include<iostream>
#include<Windows.h>
#include<time.h>
using namespace std;

void SelectSort(int *List, const int n);
int main()
{
	clock_t start_time = clock();
	int x[] = { 1,3,5,7,9,0,2,4,6,8 };
	SelectSort(x, 10);
	for (int k = 0; k < 10; k++)
		cout << x[k] << endl;
	clock_t end_time = clock();
	cout << \"程序段运行时间:\" << static_cast<double> (end_time - start_time) / CLOCKS_PER_SEC * 1000 << \"ms\" << endl;
	system(\"pause\");
	return 0;
}
void SelectSort(int *List, const int n)
{
	for (int i = 0; i < n-1; i++)
	{
		int min = i;//min处,假设第一个是最小的,是;数组的下标
		for (int j = i + 1; j < n; j++) //j=i+1是因为之前已经扫描过了
		{
			if (List[j] < List[min])
			{
				min = j;    //移动记录下来
			}
			
		}
           swap(List[i], List[min]);   //扫描一遍结束后,交换一次
	}
}

 

收藏 打印