插入排序
时间:2021-06-28 19:14:34
收藏:0
阅读:0
1.原理:
2.代码实现:
1 /* 2 * insert:实现插入排序 3 * ary[ARRAY] 需要排序的数组 4 * @return 5 * [ARRAY] 排序后的新数组 6 * by js0205 7 */ 8 function insert(ary) { 9 let handle = []; 10 handle.push(ary[0]); 11 for (let i = 0; i < ary.length; i++) { 12 let A = ary[i]; 13 for (let j = handle.length - 1; j >= 0; j--) { 14 let B = handle[j]; 15 if (A > B) { 16 handle.splice(j + 1, 0, A); 17 break; 18 } 19 if (j === 0) { 20 handle.unshift(A); 21 } 22 } 23 } 24 return handle; 25 } 26 let ary = [12, 8, 24, 16, 1]; 27 handle = insert(ary); 28 console.log(handle);
3.时间复杂度:
空间复杂度:
评论(0)