您的位置:首页 > 其它

【算法】插入排序(Insertion Sort)

2013-11-24 17:57 561 查看
(PS:内容参考MIT算法导论)

插入排序(Insertion Sort):

适用于数目较少的元素排序

伪代码(Pseudocode):



例子(Example):



符号(notation):



时间复杂度(Running Time):



源代码(Source Code):

#include<iostream>

using namespace std;

template<class T>

void InsertSort(T a[], int n){

for(int j=1;j<n;j++){

T t=a[j];

int i=j-1;

while(i>=0 && a[i]>t){

a[i+1]=a[i];

i=i-1;

}

a[i+1]=t;

}

}

void main(){

int a[]={8,2,4,9,3,6};

InsertSort(a,6);

cout<<"after insertionSort: "<<endl;

for(int i=0;i<6;i++)

cout<<a[i]<<" ";

cout<<endl;

system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: