java:直接插入排序

import java.util.Arrays;

public class testinsertsort {
public static void main(String[] args) {
int[] arr = { 243, 45, 78, 355, 322, 235 };
insertsort(arr);
System.out.println(Arrays.toString(arr));
}

public static void insertsort(int[] arr) {
// 从左至右遍历所有数
for (int i = 1; i < arr.length; i++) {
// 如果当前数小于前一个数
if (arr[i] < arr[i - 1]) {
int temp = arr[i];// 则把当前数存起来
int j;
// 遍历当前数前面所有的数字(从右至左)
for (j = i - 1; j >= 0 && temp < arr[j]; j--) {
// 把前一个数赋给后一个数
arr[j + 1] = arr[j];
}
// 把外层的当前元素(temp)赋值给不满足条件的后一个元素
arr[j + 1] = temp;
}
}

}
}

原文地址:https://www.cnblogs.com/oycq9999/p/10239394.html