C++数组--直接选择排序

//从左到右逐个排序
#include<iostream>
#include<cstdio>
using namespace std;
int a[10]={
    
    2,52,45,63,98,25,12,36,5,47};
int main()
{
    
    
	for(int i=0;i<10;i++){
    
    
		for(int j=i;j<10;j++){
    
    
			int b;
			if(a[j]<a[i]){
    
    
				b=a[j];
				a[j]=a[i];
				a[i]=b;
			}
			/*发现比 a[i] 小的数就将它们交换位置*/
		}
	}
	for(int n=0;n<10;n++){
    
    
		cout<<a[n]<<endl;
	}
	return 0;
}
 

输出:

2
5
12
25
36
45
47
52
63
98

仔细想想,其实这不就是题目 ASC II码排序 的升级版吗?

猜你喜欢

转载自blog.csdn.net/interestingddd/article/details/113825545
今日推荐