package com.atguigu.sort;
import java.util.Arrays;
/**
* 插入排序
* @author Dxkstart
* @create 2021-10-11-18:45
*/
public class InsertSort {
public static void main(String[] args) {
int[] arr = {101,34,119,1};
insertSort(arr);
}
//插入排序
public static void insertSort(int[] arr) {
int insertVal;//定义待插入的数
int insertIndex;//即arr[1]的前面这个数的索引
for (int i = 0; i < arr.length - 1; i++) {
insertVal = arr[i+1];
insertIndex = i;
while (insertIndex >= 0 && insertVal < arr[insertIndex]){
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
arr[insertIndex + 1] = insertVal;
}
System.out.println("排序结果为:");
System.out.println(Arrays.toString(arr));
}
}