插入排序
1 #include<iostream> 2 using namespace std; 3 void InsertSort(int a[],int n) 4 { 5 for(int i = 1;i<n;i++) 6 { 7 int k = i; 8 int pre = i-1; 9 int cur = a[i]; 10 while(pre >= 0 && cur<a[pre]) 11 { 12 a[pre+1] = a[pre]; //后移 13 pre--; 14 } 15 a[pre+1] = cur; 16 } 17 } 18 19 int main() 20 { 21 int a[10] = {2,5,9,6,4,3,5,10,7,6}; 22 InsertSort(a,10); 23 for(int i = 0;i<10;i++) 24 { 25 cout << a[i] <<" "; 26 } 27 }

浙公网安备 33010602011771号