console
const btn1 = document.getElementById('btn1')
btn1.addEventListener('click',()=>{
console.log('>>>>>')
})
function func1(arr, n) {
let left = 0;
let right = arr.length - 1;
let zx = arr[left] < arr[right];
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === n) {
return mid;
} else if (zx ? arr[mid] < n : arr[mid] > n) {
left = mid + 1;
} else {
right = mid - 1;
}
}
arr.splice(left, 0, n);
return left;
}
const increasingArr = [1, 3, 5, 7, 9];
const n = 6;
const index = func1(increasingArr, n);
console.log(`数字 ${n} 应插入到位置 ${index}`);
console.log(increasingArr);
const decreasingArr = [9, 7, 5, 3, 1];
const n2 = 6;
const index2 = func1(decreasingArr, n2);
console.log(`数字 ${n2} 应插入到位置 ${index2}`);
console.log(decreasingArr);
<div id="parent">
<div id="btn1" ></div>
</div>
#parent {
width: 200px;
height: 200px;
background: #999;
pointer-events: none;
}
#btn1 {
width: 40px;
height: 40px;
background: #fe1;
}