插入排序

一、插入排序

1.直接插入排序

直接插入排序是一种最简单的排序方法,它的基本操作是将一个记录插入到已排好序的有序表中,从而得到一个新的记录数增1的有序表。

基本思想:首先,我们将数组中的数据分为两个区间,已排序区间和未排序区间。初始已排序区间只有一个元素,就是数组的第一个元素。插入算法的核心思想是取未排序区间中的元素,在已排序区间中找到合适的插入位置将其插入,并保证已排序区间数据一直有序。重复这个过程,直到未排序区间中元素为空,算法结束。

public class InsertSort {

    public static int[] insertSort(int[] array) {
        int tmp;
        for (int i = 1; i < array.length; i++) {
            // 0到i-1都已有序,i依次与前面比较
            for (int j = i; j > 0; j--) {
                if (array[j] < array[j - 1]) {
                    // 交换
                    tmp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = tmp;
                }
            }
        }
        return array;
    }

    public static void main(String[] args) {
        int[] array = { 1,3, 5, 7, 9, 4, 6, 10, 2, 8 };
        int[] resultArray = insertSort(array);

        for (int i : resultArray) {
            System.out.print(i + " ");//1 2 3 4 5 6 7 8 9 10 
        }
    }
}

直接插入排序算法简便,且容易实现。当待排序记录数量很小时,这是一种很好的排序方法。但是通常待排序序列中记录数量很大,则不宜采用直接插入排序。可以使用折半插入排序等进行改进。

时间复杂度

 

2.折半插入排序

由于插入排序的基本操作是在一个有序表中进行查找和插入的,这个查找操作可利用折半查找来实现,由此进行的插入排序称之为折半插入排序。

public class InsertSort {

    public static int[] insertSort(int[] array) {
        int tmp;
        for (int i = 1; i < array.length; i++) {
            // 0到i-1都已有序,i依次与前面比较
            for (int j = i; j > 0; j--) {
                if (array[j] < array[j - 1]) {
                    // 交换
                    tmp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = tmp;
                }
            }
        }
        return array;
    }

    public static void main(String[] args) {
        int[] array = { 1, 3, 5, 7, 9, 4, 6, 10, 2, 8 };
        int[] resultArray = insertSort(array);

        for (int i : resultArray) {
            System.out.print(i + " ");// 1 2 3 4 5 6 7 8 9 10
        }
    }
}

折半插入排序算法的时间复杂度为O(n^2)

3. 2-路插入排序

见《数据结构-严蔚敏》10.2.2节P266

 

二、shell排序

希尔排序又称为缩小增量排序,它也是属于插入排序的一种,但在时间复杂度上有了较大改进。

基本思想:先将整个待排记录序列分割成若干个子序列,分别进行直接插入排序,待整个序列中的记录基本有序时,再对全体记录进行一次直接插入排序。

/**
 * shell排序
 *
 */
public class ShellSort {
    
    public static int[] shellSort(int[] array) {
        double d1 = array.length;
        int temp = 0;
        while (true) {
            d1 = Math.ceil(d1 / 2);
            int d = (int) d1;
            for (int x = 0; x < d; x++) {
                for (int i = x + d; i < array.length; i += d) {
                    int j = i - d;
                    temp = array[i];
                    for (; j >= 0 && temp < array[j]; j -= d) {
                        array[j + d] = array[j];
                    }
                    array[j + d] = temp;
                }
            }
            if (d == 1)
                break;
        }
        return array;

    }

    public static void main(String[] args) {
        int array[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };

        shellSort(array);

        for (int element : array)
            System.out.print(element+" ");
    }
}

 

posted @ 2017-07-12 23:57  静水楼台/Java部落阁  阅读(172)  评论(0编辑  收藏  举报