插入排序

插入排序的基本思想: 从表中第二项开始,每次将此项按其大小插入到前面已经排序的数据中的适当位置,直到全部插入完毕。 

有两种方法可以实现插入排序, 第一种方法,用一个拷贝表,表的大小和原表一致,但是没有数据, 将原表中的数据从第一项开始,把项放到拷贝表中,再插入原表的第二项,按照大小,可能插入,或者直接放到第一项后面,然后原表第三项,第四项依次操作,直到拷贝表填满, 然后把拷贝表数据复制到原表, 原表就排列好了。 第二种方法, 不占用额外的空间, 直接在原表进行操作。 依次把第2项,第2项,... 第 n 项作为主元,把主元项移到一个临时位置, 让主元项变成空位置。 然后循环让主元的前面项和主元比较,如果比主元大,则向后移动,到空位置上去, 把该项设置成空位置,最后让主元占据空位置。 这样的过程重复 n - 1 遍,列表就全部排序完成了。 

第一个方法相对更简单一点,直接上代码: 

public class insertSort 
{
    public static void main(String[] args)
    {
        int[] b = {7, 7, 5, 11, 2, 16, 4, 18, 22, 32, 1};
        
        System.out.println("Array values before sorting: ");
        int i; 
        for (i = 0; i < b.length; i++) 
            System.out.print(b[i] + " ");
        System.out.println();
        
        insertSort(b);
        
        System.out.println("Array values after sorting: ");
        for (i = 0; i < b.length; i++) 
            System.out.print(b[i] + " ");
        System.out.println();
    }
    
    private static void insertSort (int[] array) 
    {
        int[] temp = new int[array.length];   //创建一个拷贝数组。
        int index, position;  //下标变量。 
        position = 0;   //position 变量跟踪 temp 中插入了多少元素。 初始化为0, 表示还没有插入元素。 
        for (index = 0; index < array.length; index++)
        {
            insertValuetoTemp(array[index],temp, position);   //将 array[index] 的值插入到 temp 数组中去。
            position++;
        }
        copyTempToArray(array, temp);    //把 temp 排好的序列拷贝到 array 中去。 
    }
    
    private static void insertValuetoTemp (int value, int[] temp, int position)
    {
        if (position == 0)  //temp 还没有插入任何数值。  
            temp[position] = value;
        else if (value >= temp[position - 1]) //如果 value 的值比排好序的任何值都大,则放在最后。  
        {
            temp[position] = value; 
        }
        else               //将 value 的值插入到已排列好的数组中去。
        { 
            for (int i = 0; i < position; i++)
            {
                if (value < temp[i])  
                {
                    //将 temp[i] 直到 temp[position - 1] 全部往后挪一格。 
                    for (int j = position - 1; j >= i; j--) 
                    {
                        temp[position--] = temp[j];   
                    }
                    temp[i] = value; 
                    break;   //退出循环。 
                }
            }
        }
        
    }
    private static void copyTempToArray (int[] array, int[] temp) 
    {
        for (int i = 0; i < array.length; i++) 
        {
            array[i] = temp[i];
        }
    }
}

 第二个方法:

按照思路写出伪代码:

  N 赋值为 2.

  while (N 的值不超过 n) 

    把第 N 项作为主元项; 

    把主元项移到一个临时位置, 使得该列表留出一个空位置; 

    while (这个空位置前还有项,且此项比主元大) 

       do: 把此项往后移到空位置, 把该项的原位置设置成空位置。 

    把主元查到空位置上去; 

  N = N+1;  

 写出对应的代码: 

  

public class InsertSort 
{
    public static void main(String[] args) 
    {
        int[] array = {51, 22, 32, 41, 68, 78, 77, 99, 102, 48};
        insertSort(array);
        for (int i = 0; i < array.length; i++)
        {
            System.out.print( array[i] + " ");
        }
    }
    
    private static void insertSort (int[] array) 
    {
        int n = 1;   //取 array 的第二项值。 
        int pivot;   //array 的主元。
        int flag;    //falg 作为下标数值标记“空位置”。
        while (n < array.length)
        {
            //把列表的 第 n项作为主元项。 
            pivot = array[n];
            //把主元项移到一个临时位置, 并使该位置作为空位置。 
            int temp = pivot; 
            flag = n;   
            //如果“空位置”前面存在一个数值,且比 主元大的话: 
            while ((flag - 1 >= 0) && array[flag - 1] > pivot)
            {
                array[flag] = array[flag - 1];  // 把 a[flag - 1] 填到“空位置” 上去。
                flag = flag - 1;     //标记 空位置的下标。 
            }
            array[flag] = pivot;   //把主元 填到“空位置”上去。 
            n = n + 1; 
        }
    }
}
View Code

 

posted @ 2016-11-22 21:25  hunter_dave  阅读(167)  评论(0编辑  收藏  举报