02_插入排序
package algorithm; import java.util.Arrays; /** * 插入排序:时间复杂度为(2+3+4+...+n)~O(n2) * 思想:假定前边的已经是有序的,将当前的数值插入到合适的位置,注意大的数往后边移动 * * @author cloud * @data 2016年7月18日 * @version 1.0 * @description */ public class InsertSort { /** * @param array */ public static void insertSort(int[] array) { for (int i = 1; i < array.length; i++) { int currentValue = array[i];// 获取要插入的数据 int position = i;// 记录原始位置 for (int j = i - 1; j >= 0; j--) { if (currentValue < array[j]) {//如果当前值比前面的小,前面的数值就往后移动,要插入的位置position-- array[j + 1] = array[j]; position--; } else { break;//移动到正确位置为止 } } //把要插入的值插入到正确的位置 array[position] = currentValue; } System.out.println(Arrays.toString(array)); } public static void main(String[] args) { int[] array = { 1, 6, 9, 8, 7, 5, 3, 2, 4 }; insertSort(array); } }
    http://www.cnblogs.com/makexu/

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号