排序――插入排序
插入排序的基本思想是每次將一個(gè)待排序的記錄,按其關(guān)鍵字大小插入到前面已經(jīng)排好序的子文件中的適當(dāng)位置,直到全部記錄插入完成為止。常見(jiàn)的插入排序有插入排序(Insertion Sort),希爾排序(Shell Sort),二叉查找樹(shù)排序(Tree Sort),圖書(shū)館排序(Library Sort),Patience排序(Patience Sort)。
簡(jiǎn)單實(shí)例:
#include <iostream>using namespace std;void InsertSort( int k[], int n ){ int i, j,temp; for( i=1; i < n;i++ ) { if( k[i] < k[i-1] ) { temp = k[i]; for( j=i-1; k[j] > temp;j-- ) //找位置并且向后推移 { k[j+1] = k[j]; } k[j+1] = temp; } }}int main(){ int i ,a[10] = {5,2,6,0,3,9,1,7,4,8}; InsertSort(a,10); for( i=0; i < 10 ;i++ ) { cout << a[i]; } cout << endl; return 0;}感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答