在已经排好顺序的元素中找到当前待排元素应该插入的位置并将其插入
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 1; i < a.length; i++) {
int key = a[i], j = i;
while (j - 1 >= 0 && a[j - 1] > key) {
a[j] = a[j - 1];
j--;
}
a[j] = key;
}
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}