插入排序

1、平均时间复杂读O(n2);

2、基本思想:在数组中,假定前n-1个数已经排号序,现在把第n个数插入到前面有序的数组中,如此反复;

3、过程:假定前j个数是有序的,把第j + 1个数插入到前面的有序数组中;

4、代码:

 public void insertSort(int[] array, int length){
        for(int i = 0; i < length - 1; i++) {
            //插入的过程
            for(int j = i + 1; j > 0; j--) {
                if(array[j] < array[j - 1]) {
                    int temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }else {
                    break;
                }
            }
        }
    }

 

posted @ 2019-02-27 11:13  特仑苏灬  阅读(93)  评论(0编辑  收藏  举报