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 ...

sorting by using array

【POJ】1094 Sorting It All Out 拓扑排序

Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions:...

sorting排序算法

  • 2012-11-05 09:20
  • 15KB
  • 下载

an o(nlogn) sorting network

  • 2011-01-09 08:22
  • 540KB
  • 下载

Sorting Algorithm-Quick Sort

Quick Sort-快速排序 快速排序(Quick Sort)的基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行...

js-sorting-algorithm

  • 2017-07-09 23:04
  • 916KB
  • 下载

耐心排序Patience Sorting

这个排序的关键在建桶和入桶规则上 建桶规则:如果没有桶,新建一个桶;如果不符合入桶规则那么新建一个桶 入桶规则:只要比桶里最上边的数字小即可入桶,如果有多个桶可入,那么按照从左到右的顺序入桶即可
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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