插入排序算法

            感谢《算法导论》这本书,让我对算法有了新的认识

一.介绍

           插入排序是一个对少量元素进行排序的有效算法。插入排序的工作机理和很多人打牌时,整理手中的牌时差不多。在开始摸牌时,我们的左手是空的,牌面朝下放在桌上。接着,一次从桌上摸起一张牌,并将它插入到左手一把牌中的正确位置上。为了找到这张牌的正确位置,要将它与手中已有的每一张牌从右到左地进行比较。无论在什么时候,左手中的牌都是排好序的,而这些牌原先都是在桌上那副牌里最顶上的一张牌。

          插入排序算法的伪代码是以一个过程的形式给出的,称为insert-sort,它的参数是一个数组A【1....n】,包含了n个待排的数。输入的各个数字是原地排序的(sorted in place),意即这些数字就是在数组A中进行重新排序的,在任何时刻,至多只有其中的常熟个数字是存储在数组之外的。当过程insert-sort执行完毕后,输入数组A就包含了已经排好序的输出序列。

  下面给出了算法的java代码以及伪代码。
            
INSERT-SORT(A)
 for j ← 2 to length[A]
    do key <-A[j]   //insert A[j]into the sorted sequence A[1..j-1]
      i←j-1
       while i>0 and A[i]>key
          do A[i+1]←A[i]
             i←i-1
        A[i+1]←key
       public int[]  insertsort(int  [] A){
		int key;
		for (int j=1;j
   
    =0&&A[i]>key){
				 A[i+1]=A[i];
				 i=i-1;
			
			 }
			 	 A[i+1]=key;
		}
		
		return A ;
	}
   



二.算法分析

    摘自维基百科的关于该算法的一些优点

  • Simple implementation: Bentley shows a three-line C version, and a five-line optimized version[1]:116
  • Efficient for (quite) small data sets, much like other quadratic sorting algorithms
  • More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort
  • Adaptive, i.e., efficient for data sets that are already substantially sorted: the time complexity isO(nk) when each element in the input is no more than k places away from its sorted position
  • Stable; i.e., does not change the relative order of elements with equal keys
  • In-place; i.e., only requires a constant amount O(1) of additional memory space
  • Online; i.e., can sort a list as it receives it

   下面图解(图来自维基百科)



 

    

    

  

    下面给出维基百科上关于该算法的分析

   

The best case input is an array that is already sorted. In this case insertion sort has a linear running time (i.e., O(n)). During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection of the array.

The simplest worst case input is an array sorted in reverse order. The set of all worst case inputs consists of all arrays where each element is the smallest or second-smallest of the elements before it. In these cases every iteration of the inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. This gives insertion sort a quadratic running time (i.e., O(n2)).

The average case is also quadratic, which makes insertion sort impractical for sorting large arrays. However, insertion sort is one of the fastest algorithms for sorting very small arrays, even faster than quicksort; indeed, good quicksort implementations use insertion sort for arrays smaller than a certain threshold, also when arising as subproblems; the exact threshold must be determined experimentally and depends on the machine, but is commonly around ten.

     翻译成中文解释一下:

     1.最好的情况是数组中的数已经排好了,由于while循环内的循环体不会执行,只会执行while测试。因此这种情况下插入排序有一个最少的时间复杂度0(n)

     2.最差的情况是相反的顺序,这种情况下while循环循环体每次都会执行,因此,时间复杂度计算可得为O(n^2);

     3.平均情况的时间复杂度依然是二次的,这样的话插入排序不太适合大量的数进行排序,但是插入排序是处理小数组的最快的算法之一,甚至在处理小数组时比快速排序还要快。事实上,当数组规模小于一定的值,快排的实现往往使用了插入排序。具体这个规模一般取决于机器,但是一般是10。

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值