【算法】插入排序(Insertion Sort)
2013-11-24 17:57
561 查看
(PS:内容参考MIT算法导论)
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");
}
插入排序(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");
}
相关文章推荐
- 【算法】插入排序 insertion_sort
- 算法总结JS版(三)—— 插入排序(Insertion Sort)
- 【算法导论学习-001】插入排序(InsertionSort)
- 【DS】排序算法之插入排序(Insertion Sort)
- 算法-插入排序(Insertion Sort)
- 静态链表插入排序(List Insertion Sort)算法
- 算法:插入排序(Insertion Sort)
- 插入排序(insertion sort)算法实现
- [算法练习]Insertion Sort 插入排序
- 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结
- 算法学习记录-排序——插入排序(Insertion Sort)
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- 经典算法(8)- 插入排序(Insertion Sort) 及三个基本排序算法的比较
- 算法从排序开始——插入排序(Insertion Sort)C语言及Java实现
- 【算法】插入排序 insertion_sort
- Python 数据结构与算法——插入排序(insertion sort)
- 数据结构&算法(PHP描述) 半折插入排序 straight binary sort
- Java 插入排序(Insertion Sort)
- 插入排序(InsertionSort)
- 排序算法 之 插入排序InsertionSort