顺序查找:
function seqSearch(arr, data) {
for(let i = 0; i< arr.length;i++) {
if(data == arr[i]) {
return i;
}
}
return -1
}
var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
console.log(seqSearch(arr, 15))
二分查找:
function binSearch(arr, data) {
let low = 0;
let high = arr.length -1;
while(low < high) {
let middle = Math.floor((low+ high)/2);
if(arr[middle] == data) {
return middle
} else if(arr[middle] <data) {
low = middle +1;
} else {
high = middle -1;
}
}
return -1;
}