import java.util.Arrays;
public class InsertSort {
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++){
int insertVal = arr[i];
int insertIndex = i - 1;
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
arr[insertIndex + 1] = insertVal;
System.out.println("第"+i+"轮插入:");
System.out.println(Arrays.toString(arr));
}
}
public static void main(String[] args) {
int []arr = {101,34,119,1,-1,3};
insertSort(arr);
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-122.html