zoukankan      html  css  js  c++  java
  • 直接插入排序

    插入排序直接插入排序(Straight Insertion Sort)

    基本思想:

    将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表。即:先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插入,直至整个序列有序为止。

    要点:设立哨兵,作为临时存储和判断数组边界之用。

    直接插入排序示例:

     

    如果碰见一个和插入元素相等的,那么插入元素把想插入的元素放在相等元素的后面。所以,相等元素的前后顺序没有改变,从原无序序列出去的顺序就是排好序后的顺序,所以插入排序是稳定的。

    算法的实现:

    package method;
    
    /**
     * Created by Administrator on 2017/10/10.
     */
    public class InsertSort {
        public static void print(int[] a, int n ,int i){
            System.out.print(i);
            System.out.print(":");
            for(int j= 0; j<n; j++){
                System.out.print(a[j]);
                System.out.print(" ");
            }
            System.out.println();
        }
        public static void InsertSort(int[] arr){
            int i, j;
            int n = arr.length;
            int target;
            //假定第一个元素被放到了正确的位置上
            //这样,仅需遍历1 - n-1
            for (i = 1; i < n; i++){
                j = i;
                target = arr[i];
                while (j > 0 && target < arr[j - 1]){
                    arr[j] = arr[j - 1];
                    j--;
                }
                arr[j] = target;
                //打印每趟排序的结果
                print(arr,n,i);
            }
        }
        public static void InsertSort1(int[] a, int n){
            for(int i= 1; i<n; i++){
                //若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入
                if(a[i] < a[i-1]){
                    int j= i-1;
                    //复制为哨兵,即存储待排序元素
                    int x = a[i];
                    //先后移一个元素
                    a[i] = a[i-1];
                    //查找在有序表的插入位置
                    while( j >=0 && x < a[j]){
                        a[j+1] = a[j];
                        //元素
                        j--;
                    }
                    //插入到正确位置
                    a[j+1] = x;
                }
                //打印每趟排序的结果
                print(a,n,i);
            }
        }
        public static void main(String[] args){
            int[] a = {3,1,5,7,2,4,9,6};
            //求数组长度
            int n =a.length;
            System.out.println(n);
            //InsertSort(a);
            InsertSort1(a,n);
            //打印最终结果
            print(a,n,n);
        }
    }

    结果

    8
    1:1 3 5 7 2 4 9 6
    2:1 3 5 7 2 4 9 6
    3:1 3 5 7 2 4 9 6
    4:1 2 3 5 7 4 9 6
    5:1 2 3 4 5 7 9 6
    6:1 2 3 4 5 7 9 6
    7:1 2 3 4 5 6 7 9
    8:1 2 3 4 5 6 7 9

    Process finished with exit code 0

    时间复杂度:O(n^2).

    其他的插入排序有二分插入排序,2-路插入排序。

    梦想还是要有的,万一实现了呢!
  • 相关阅读:
    Java实现 LeetCode 30 串联所有单词的子串
    Java实现 LeetCode 29 两数相除
    Java实现 LeetCode 29 两数相除
    Java实现 LeetCode 29 两数相除
    Java实现 LeetCode 28 实现strStr()
    Java实现 LeetCode 28 实现strStr()
    Java实现 LeetCode 28 实现strStr()
    Java实现 LeetCode 27 移除元素
    Java实现 LeetCode 27 移除元素
    字符编码终极笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian
  • 原文地址:https://www.cnblogs.com/jianfeijiang/p/7644377.html
Copyright © 2011-2022 走看看