2017-08-17 11:40:36
writer:pprp
最简单的插入排序,用代码实现思想
1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 #include <cstring> 5 6 using namespace std; 7 const int maxn = 9999; 8 int a[maxn]; 9 int N; 10 11 void init() 12 { 13 memset(a,0,sizeof(a)); 14 for(int i = 1 ; i <= N ; i++) 15 { 16 cin >> a[i]; 17 } 18 } 19 20 void insert_sort() 21 { 22 init(); 23 24 for(int i = 2 ; i <= N ; i++) 25 { 26 int key = a[i]; 27 int j = i - 1; 28 while(j > 0 && a[j] > key) 29 { 30 a[j + 1] = a[j]; 31 j--; 32 } 33 34 a[j + 1] = key; 35 } 36 37 for(int i = 1 ; i <= N ; i++) 38 cout << a[i] << " "; 39 } 40 41 int main() 42 { 43 cin >> N; 44 insert_sort(); 45 46 return 0; 47 }