给一个数组和一个数字,将数字插入数字指定位置,使数组有序,不使用遍历
function insert(array, num) {
var middle = Math.round(array.length / 2);
var left = 0;
var right = array.length
while (right - left > 1) {
if (num <= array[middle]) {
right = middle;
} else {
left = middle;
}
middle = Math.round((right + left) / 2);
}
array.splice(middle, 0, num);
console.log(array);
}