03-插入排序算法
插入排序
插入排序:每次將一個(gè)待排序的記錄,按其關(guān)鍵字的大小插入到前面已經(jīng)排續(xù)好的數(shù)組的適當(dāng)位置上,直到全部插入為止。時(shí)間復(fù)雜度為
插入排序代碼
public?static?void?insertSort(int[]?array,?int?len){????for?(int?i?=?1;?i?<?len;?i++){
????????int?j?=?i;
????????int?temp?=?array[i];?//將temp值往前遍歷插入到合適的位置
????????while?(j?>?0?&&?array[j-1]?>?temp){
???????????array[j]?=?array[j-1];
???????????j--;
????????}
????????array[j]?=?temp;
????}
}
測(cè)試樣例
- 2 1 5 4 3
第2次遍歷之后數(shù)組為:1?2?5?4?3?
第3次遍歷之后數(shù)組為:1?2?4?5?3?
第4次遍歷之后數(shù)組為:1?2?3?4?5?
?
----- end -----
?
轉(zhuǎn)載于:https://www.cnblogs.com/denluoyia/p/9673498.html
總結(jié)
- 上一篇: mysql-视图、触发器、事务、存储过程
- 下一篇: [Poetize6] IncDec Se