public class InsertionSort { /** * 插入排序 * @param args */ public static void main(String[] args) { int[] sourceArray = {22,42,12,73,24,15,31,27,48,9}; InsertionSort is = new InsertionSort(); int[] targetArray = is.sort(sourceArray); for (int i = 0; i < targetArray.length; i++) { int j = targetArray[i]; System.out.println(j); } } private int[] sort(int [] sourceArray){ for(int i=1; i<sourceArray.length; i++){ int temp = sourceArray[i];//将要比较的数先拎出来 int j = i;//想象把数组切成两部分sourceArray[j]为已经排好序的 while((j>0) && (temp<sourceArray[j-1])){ sourceArray[j] = sourceArray[j-1]; j--; } sourceArray[j] = temp;//把拎出来的数放回到正确的位置 } return sourceArray; } }砖!!
(排序算法一)插入排序
自己写的,欢迎拍
猜你喜欢
转载自renegade24.iteye.com/blog/1663312
今日推荐
周排行