insert-sorting

原创 2007年09月23日 22:45:00

Introduction to Algorithms, 2nd Edition, P19

Discriptions:

Input: A sequence of n numbers a1, a2, . . .,an.
• Output: A permutation (reordering) a1', a2', ..., an'of the input sequence such that a1' <= a2' <=...<=an'.

Pseudcodes

INSERTION-SORT(A)
1 for j ← 2 to length[A]
2 do key ← A[j]
3 ▹ Insert A[j] into the sorted sequence A[1 j - 1].
4 i ← j - 1
5 while i > 0 and A[i] > key
6 do A[i + 1] ← A[i]
7 i ← i - 1
8 A[i + 1] ← key

My codes in C++:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#define MAX 100

int main()
...{
    
int A[MAX]; //定义数组

    
int i=0;
    
int j,key,num; 
    
    
/**//*输入数组*/
    
while(cin >> A[i])
        i
++;      
    num
=i;  //记录数组元素个数

    
for(j=1; j< num; j++)  //从第二个元素开始插入
    ...{
        key 
= A[j];
        
for(i = j-1; i>=0 && A[i] > key; --i) 
            A[i
+1]=A[i];
            A[
++i]=key;
                
    }


    
for(i=0; i< num; i++)
        cout 
<< A[i] <<endl;
    
    
return 0;

}

 Analysis:

INSERTION-SORT(A) cost times
1 for j ← 2 to length[A] c1 n
2 do key ← A[j] c2 n - 1
3 ▹ Insert A[j] into the sorted
sequence A[1 j - 1]. 0 n - 1
4 i ← j - 1 c4 n - 1
5 while i > 0 and A[i] > key c5
6 do A[i + 1] ← A[i] c6
7 i ← i - 1 c7
8 A[i + 1] ← key c8 n - 1
The running time of the algorithm is the sum of running times for each statement executed; a
statement that takes ci steps to execute and is executed n times will contribute cin to the total
running time.[5] To compute T(n), the running time of INSERTION-SORT, we sum the
products of the cost and times columns, obtaining

Even for inputs of a given size, an algorithm's running time may depend on which input of
that size is given. For example, in INSERTION-SORT, the best case occurs if the array is
already sorted. For each j = 2, 3, . . . , n, we then find that A[i] ≤ key in line 5 when i has its
initial value of j - 1. Thus tj = 1 for j = 2, 3, . . . , n, and the best-case running time is
T(n) = c1n + c2(n - 1) + c4(n - 1) + c5(n - 1) + c8(n - 1)
= (c1 + c2 + c4 + c5 + c8)n - (c2+ c4 + c5 + c8).

If the array is in reverse sorted order-that is, in decreasing order-the worst case results. We
must compare each element A[j] with each element in the entire sorted subarray A[1 j - 1],
and so tj = j for j = 2, 3, . . . , n. Noting that n*(n+1)/2-1.This worst-case running time can be expressed as an2 + bn + c for constants a, b, and c that again depend on the statement costs ci ; it is thus a quadratic function of n.

 

C Tips: 排序算法:插入排序(Insert sorting)

代码如下: void InsertSort(void** array, size_t count, int(*cmp)(const void *, const void *)) { size_t ...
  • XinYaping
  • XinYaping
  • 2014年04月05日 17:35
  • 2831

OCJP(1Z0-851) 模拟题分析(六)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考...
  • xujinsmile
  • xujinsmile
  • 2013年02月10日 21:05
  • 8204

【unity小技巧之四】变量弹出SortingLayer选择面板

经常想在脚本中设置一个变量对应SortingLayer的某个层,然后赋值给SpriteRender修改排序层,但是unity官方似乎没有办法直接弹出SortingLayer面板进行选择,就像是Laye...
  • egostudio
  • egostudio
  • 2016年06月24日 11:28
  • 2394

sorting by using array

  • 2014年12月12日 14:33
  • 2KB
  • 下载

Cell sorting in a Petri dish controlled by computer vision

  • 2014年06月20日 21:35
  • 3.78MB
  • 下载

sorting排序算法

  • 2012年11月05日 09:20
  • 15KB
  • 下载

an o(nlogn) sorting network

  • 2011年01月09日 08:22
  • 540KB
  • 下载

js-sorting-algorithm

  • 2017年07月09日 23:04
  • 916KB
  • 下载

External sorting

  • 2008年06月01日 21:23
  • 94KB
  • 下载

sorting and searching algorithms(Thomas Niemann)

  • 2007年07月07日 08:38
  • 225KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:insert-sorting
举报原因:
原因补充:

(最多只允许输入30个字)