簡單插入排序
來源:程序員人生 發布時間:2014-11-20 08:48:28 閱讀次數:2264次
#include<iostream>
using namespace std;
//定義1個交換函數
void swap(int &a,int &b)
{
int tmp;
tmp=a;
a=b;
b=tmp;
}
void InsertSort(int *A,int length)
{
int i,j;
for(int i=1;i<length;++i)
{
if(A[i]<A[i⑴])
{
int j=i;
while(A[j]<A[j⑴]&&j>=1)
{
swap(A[j],A[j⑴]);
j--;
}
}
}
}
int main()
{ int a[9]={9,1,5,8,3,7,4,6,2};
InsertSort(a,9);
for(int i=0;i<9;++i)
cout<<a[i]<<" ";
cout<<endl;
system("pause");
return 0;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈