插入排序

package day;

import java.util.Arrays;
/**
 * 插入排序
 * 从index为1的位置插入左边队列,要插入的数字做一个变量保存
 * @author Administrator
 *
 */
public class InsertSort {
    public static void insertSort(int[] array){
        
        for(int i = 1;i < array.length; i++){
            int j = i;
            int temp = array[i];
            while(j - 1 >= 0 && array[j - 1] > temp){
                array[j] = array[-- j];
            }
            array[j] = temp;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a = {1,-2,3,-4,5,-6,7,-8,9};
        insertSort(a);
        System.out.println(Arrays.toString(a));
    }
}
原文地址:https://www.cnblogs.com/caobojia/p/6777751.html