public class InsertSort { public static void main(String[] args) { int[] arr = {58, 5, 10, 2, 47}; for (int i = 1; i < arr.length; i++) { int num = arr[i]; //查找要插入的位置! int j; for (j = i - 1; j >= 0; j--) { if (num < arr[j]) { //后移 arr[j + 1] = arr[j]; } else { break; } } //插入:刚刚比较的索引后一个! arr[j + 1] = num; } System.out.println(Arrays.toString(arr)); } }
标签:arr,int,插入排序,插入,num,public From: https://www.cnblogs.com/19981206-zxb/p/16889509.html