1.选择排序

1.选择排序

源代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int N=5;
	int a[N]={5,4,3,2,1};
	for(int i=0;i<N;i++)
	{
		int k=i;		//有序数组的下一位||无序数组的第一位
		for(int j=i;j<N;j++)
		{
			if(a[j]<a[k]) k=j;  //找到无序数组中最小的 
		}
		int temp=a[i];   //无序数组中最小的 与 无序数组中第一位 交换
		a[i]=a[k];
		a[k]=temp;
	}
	for(int i=0;i<N;i++) cout<<a[i];   
	return 0;
}

测试结果:

12345
--------------------------------
Process exited after 0.01276 seconds with return value 0

发布了3 篇原创文章 · 获赞 3 · 访问量 32

猜你喜欢

转载自blog.csdn.net/xg987599519/article/details/103945511