InsertSort 插入排序
插入排序:将下一个插入已排好的序列中
自己觉得演示的号的一个文章地址
http://sjjg.js.zwu.edu.cn/SFXX/sf1/zjcr.html
下面是java的实现代码:
//InsertSort public class TestInsertSort { public int[] testInsertSortArray(int[] arr){ for(int i = 1; i< arr.length; i ++){ if(arr[i] < arr[i-1]){ int temp = arr[i]; int j = i; while(j >0 && arr[j-1] > temp){ arr[j] = arr[j-1]; j --; } arr[j] = temp; } } return arr; } public static void main(String[] args) { int[] arr = {6,2,4,1,5,9}; TestInsertSort test = new TestInsertSort(); test.testInsertSortArray(arr); for(int i = 0 ; i < arr.length; i ++){ System.out.println(arr[i]); } } }
ref:
http://www.2cto.com/kf/201109/104886.html