package algorithm.sorts; public class InsertSort { public static int[] insertSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int temp = arr[i]; int j = i; while (j > 0 && temp < arr[j - 1]) { arr[j] = arr[j - 1]; j--; } arr[j] = temp; } return arr; } public static void main(String[] args) { int[] arr = {99, 22, 33, 44, 2, 5, 1, 7}; int[] result = insertSort(arr); for (int i = 0; i < result.length; i++) { System.out.println(result[i]); } } }
来源:oschina
链接:https://my.oschina.net/u/4055223/blog/4306764