For faster navigation, this Iframe is preloading the Wikiwand page for 插入排序.

插入排序

插入排序
使用插入排序为一列数字进行排序的过程
概况
類別排序算法
資料結構数组
复杂度
平均時間複雜度
最坏时间复杂度
最优时间复杂度
空間複雜度总共 ,需要辅助空间
最佳解No
相关变量的定义
使用插入排序为一列数字进行排序的过程

插入排序(英語:Insertion Sort)是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。

記載

最早擁有排序概念的機器出現在1901至1904年間由赫爾曼·何樂禮發明出使用基數排序法的分類機,此機器系統包括打孔,制表等功能,1908年分類機第一次應用於人口普查,並且在兩年內完成了所有的普查數據和歸檔。 赫爾曼·何樂禮在1896年創立的分類機公司的前身,為電腦製表記錄公司(CTR)。他在電腦製表記錄公司曾擔任顧問工程師,直到1921年退休,而電腦製表記錄公司在1924年正式改名為IBM

概述

Insertion Sort 和打撲克牌時,從牌桌上逐一拿起撲克牌,在手上排序的過程相同。

舉例:

輸入: {5 2 4 6 1 3}。

首先拿起第一張牌, 手上有 {5}。

拿起第二張牌 2, 把 2 insert 到手上的牌 {5}, 得到 {2 5}。

拿起第三張牌 4, 把 4 insert 到手上的牌 {2 5}, 得到 {2 4 5}。

以此類推。

算法

一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:

  1. 从第一个元素开始,该元素可以认为已经被排序
  2. 取出下一个元素,在已经排序的元素序列中从后向前扫描
  3. 如果该元素(已排序)大于新元素,将该元素移到下一位置
  4. 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
  5. 将新元素插入到该位置后
  6. 重复步骤2~5

範例程式碼

此范例程序以C语言实现。[1]

void insertion_sort(int arr[], int len){
        int i,j,key;
        for (i=1;i!=len;++i){
                key = arr[i];
                j=i-1;
                while((j>=0) && (arr[j]>key)) {
                        arr[j+1] = arr[j];
                        j--;
                }
                arr[j+1] = key;
        }
}

此范例程序以Objective C实现。[1]

- (NSMutableArray *)insertionSort:(NSArray *)array {
    NSMutableArray *sortArray = [array mutableCopy];
    NSNumber *key = @(0);
    int j = 0;
    for (int i = 1; i < sortArray.count; i++) {
        key = array[i];
        j = i - 1;
        while ((j >= 0) && [sortArray[j] integerValue] > [key integerValue]) {
            sortArray[j + 1] = sortArray[j];
            j --;
        }
        sortArray[j + 1] = key;
    }
    return sortArray;
}

Julia (程式語言)

# Julia Sample : InsertSort
function InsertSort(A)
  for i=2:length(A)
    key = A[i]
    j=i-1
    while (j>=1)&&(A[j]>key)
      A[j+1]=A[j]
      j-=1
    end
    A[j+1]=key
  end
  return A
end

# Main Code
A = [16,586,1,31,354,43,3]
println(A)               # Original Array
println(InsertSort(A))   # Insert Sort Array

算法复杂度

如果目标是把n个元素的序列升序排列,那么采用插入排序存在最好情况和最坏情况。最好情况就是,序列已经是升序排列了,在这种情况下,需要进行的比较操作需次即可。最坏情况就是,序列是降序排列,那么此时需要进行的比较共有次。插入排序的赋值操作是比较操作的次数减去次,(因为次循环中,每一次循环的比较都比赋值多一个,多在最后那一次比较并不带来赋值)。平均来说插入排序算法复杂度为。因而,插入排序不适合对于数据量比较大的排序应用。但是,如果需要排序的数据量很小,例如,量级小于千;或者若已知輸入元素大致上按照順序排列,那么插入排序还是一个不错的选择。 插入排序在工业级库中也有着广泛的应用,在STL的sort算法和stdlib的qsort算法中,都将插入排序作为快速排序的补充,用于少量元素的排序(通常为8个或以下)。

参考文献

  1. ^ 1.0 1.1 Cormen, Thomas H. 英语Thomas H. Cormen; Leiserson, Charles E. 英语Charles E. Leiserson; Rivest, Ronald L.; Stein, Clifford. Section 2.1: Insertion sort. Introduction to Algorithms 3rd. MIT Press and McGraw-Hill. 2009: 16–18 [1990]. ISBN 0-262-03384-4. .

延伸閱讀

  • [97严] 严蔚敏,吴伟民,《数据结构C语言版》,清华大学出版社,1997年4月
  • [99殷] 殷人昆,陶永雷,谢若阳,盛绪华,《数据结构(用面向对象方法与C++描述)》,清华大学出版社,1999年7月
{{bottomLinkPreText}} {{bottomLinkText}}
插入排序
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?