资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200
输入格式
第一行为一个整数n。
第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000。
输出格式
输出一行,按从小到大的顺序输出排序后的数列。
样例输入
5
8 3 6 4 9
样例输出
3 4 6 8 9
提交代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++)
arr[i] = in.nextInt();
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1]) {
int t = arr[j];
arr[j]= arr[j+1];
arr[j+1]= t;
}
for(int i:arr)
System.out.print(i+" ");
}
}
冒泡顺序是从后往前整理;每一次外循环排列好一位;
https://upload-images.jianshu.io/upload_images/9916080-f0605d250bd43468?imageMogr2/auto-orient/strip|imageView2/2/w/826/format/webp