您的位置:首页 > 其它

直接插入排序举例

2010-05-31 20:50 162 查看
#include<iostream>
template<class T>
void InsertionSort(T A[],int n)
{
int i,j;
T temp;
for(i=1;i<n;i++)
{
j=i;
temp=A[i];
while(j>0&&temp<A[j-1])
{
A[j]=A[j-1];
j--;
}
A[j]=temp;
for(int k=0;k<n;k++)
cout<<A[k]<<" ";
cout<<endl;
}
}
int main()
{
int data1[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20};
cout << "排序前的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;
cout << "开始排序..." << endl;
InsertionSort(data1, 20);
cout << "排序后的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;

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