例:
输入:5
3 2 1 5 4
输出:1 2 3 4 5
解:
public class Main {
public static void insertSort(int[] a) {// 从第二个元素开始
for (int i = 1; i < a.length; i++) {
int temp = a[i];
int j = i;
while (j > 0 && temp < a[j - 1]) {// 如果temp小于前一位,则向前移
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
insertSort(a);
for (int b : a)
System.out.print(b + " ");
}
}