zoukankan      html  css  js  c++  java
  • 算法系列:插入排序

    Copyright © 1900-2016, NORYES, All Rights Reserved.

    http://www.cnblogs.com/noryes/

    欢迎转载,请保留此版权声明。

    -----------------------------------------------------------------------------------------

     

    基本思想:

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

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

        直接插入排序示例:

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

    算法实现:

     
    1. void print(int a[], int n ,int i){  
    2.     cout<<i <<":";  
    3.     for(int j= 0; j<8; j++){  
    4.         cout<<a[j] <<" ";  
    5.     }  
    6.     cout<<endl;  
    7. }  
    8.   
    9.   
    10. void InsertSort(int a[], int n)  
    11. {  
    12.     for(int i= 1; i<n; i++){  
    13.         if(a[i] < a[i-1]){               //若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入  
    14.             int j= i-1;   
    15.             int x = a[i];        //复制为哨兵,即存储待排序元素  
    16.             a[i] = a[i-1];           //先后移一个元素  
    17.             while(x < a[j]){  //查找在有序表的插入位置  
    18.                 a[j+1] = a[j];  
    19.                 j--;         //元素后移  
    20.             }  
    21.             a[j+1] = x;      //插入到正确位置  
    22.         }  
    23.         print(a,n,i);           //打印每趟排序的结果  
    24.     }  
    25.       
    26. }  
    27.   
    28. int main(){  
    29.     int a[8] = {3,1,5,7,2,4,9,6};  
    30.     InsertSort(a,8);  
    31.     print(a,8,8);  
    32. }  

     

    效率:

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

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

     weiss 代码

     1 /*
     2 * Simple insertion sort.
     3 */
     4 template <class T>
     5 void DLLALGOLIB insertionSort(std::vector<T>& coll)
     6 {
     7     for (int idx = 1; idx < (int)coll.size(); ++idx)
     8     {
     9         T tmp = coll[idx];
    10 
    11         int j = idx;
    12         for (; j > 0 && tmp < coll[j - 1]; --j)
    13         {
    14             coll[j] = coll[j - 1];
    15         }
    16 
    17         coll[j] = tmp;
    18 
    19         PRINT_ELEMENTS(coll);
    20     }
    21 }
    22 
    23 
    24 /*
    25 * STL insertion sort.
    26 */
    27 template <class Iterator>
    28 void DLLALGOLIB insertionSort(const Iterator& begin, const Iterator& end)
    29 {
    30     if (begin != end)
    31     {
    32         insertionSortHelp(begin, end, *begin);
    33     }
    34 }
    35 
    36 
    37 template <class Iterator, class Object>
    38 void DLLALGOLIB insertionSortHelp(const Iterator& begin, const Iterator& end, const Object& obj)
    39 {
    40     insertionSort(begin, end, std::less<Object>());
    41 }
    42 
    43 
    44 template <class Iterator, class Comparator>
    45 void DLLALGOLIB insertionSort(const Iterator& begin, const Iterator& end, Comparator lessThan)
    46 {
    47     if (begin != end)
    48     {
    49         insertionSort(begin, end, lessThan, *begin);
    50     }
    51 }
    52 
    53 
    54 template <class Iterator, class Comparator, class Object>
    55 void DLLALGOLIB insertionSort(const Iterator& begin, const Iterator& end, Comparator lessThan, const Object& obj)
    56 {
    57     for (Iterator idx = begin + 1; idx != end; ++idx)
    58     {
    59         Object tmp = *idx;
    60 
    61         Iterator j = idx;
    62         for (; j != begin && lessThan(tmp, *(j - 1)); --j)
    63         {
    64             *j = *(j - 1);
    65         }
    66 
    67         *j = tmp;
    68 
    69         //PRINT_ELEMENTS(coll);
    70     }
    71 }


  • 相关阅读:
    *1408素数回文数的个数
    *1406单词替换
    1404我家的门牌号
    1402Vigenère密码(Noip2012提高组第1题)
    1401机器翻译(Noip2010提高组第1题)
    AcWing
    模板
    The 2019 Asia Nanchang First Round Online Programming Contest
    模板
    模板
  • 原文地址:https://www.cnblogs.com/noryes/p/5716689.html
Copyright © 2011-2022 走看看