1.动图演示:
2.编写代码:
public static int[] insert_sort(int[] nums) {
int[] arr = Arrays.copyOf(nums, nums.length);
for (int i = 0; i < arr.length - 1; i++) {
int j = i;
int tmp = arr[j + 1];
while (j > 0 && tmp < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = tmp;
}
return arr;
}