程序员人生 网站导航

简单插入排序

栏目:互联网时间:2014-11-20 08:48:28
#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; }

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐