题解 | #排序#

排序

https://www.nowcoder.com/practice/508f66c6c93d4191ab25151066cb50ef

就是排序问题

我写的是冒泡

#include <iostream>
using namespace std;

#define N 101
int main(){
	int n;
	while(cin >> n){
		int a[N];
		for (int i = 0; i < n; i++){
			cin >> a[i];
		}
		for (int i= 0; i < n-1;i++){
			int flag = 0;
			for (int j = 0; j < n-i-1;j++){
				if (a[j]>a[j+1]){
					int temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
					flag = 1;
				}
			}
			if (!flag) break;
		}
		for (int i = 0; i < n; i++){
			cout <<a[i]<<" ";
		}
	}
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务