标签:style class blog c code java
插入排序伪代码:
1 for j=2 to a.length 2 key=a[j] 3 i=j-1 4 while i>0 and a[i]>key 5 a[i+1]=a[i] 6 i=i-1 7 a[i+1]=key
使用实例:
1 #include<iostream> 2 #include<time.h> 3 #include<stdlib.h> 4 using namespace std; 5 #define N 5 6 int main(){ 7 srand(time(NULL)); 8 int a[N]; 9 for(int i=0;i<N;++i){ 10 a[i]=rand()%20+1; 11 } 12 for(int i=0;i<N;++i){ 13 cout<<a[i]<<"\t"; 14 } 15 cout<<endl; 16 for(int j=2;j<N;++j){ 17 int key=a[j]; 18 int i=j-1; 19 while(i>0 && a[i]>key){ 20 a[i+1]=a[i]; 21 i=i-1; 22 } 23 a[i+1]=key; 24 } 25 cout<<"*****************************************\n"; 26 for(int i=0;i<N;++i){ 27 cout<<a[i]<<"\t"; 28 } 29 cout<<endl; 30 return 0; 31 }
运行结果:
标签:style class blog c code java
原文地址:http://www.cnblogs.com/qinsongli/p/3737441.html