- 顺序搜索
function sequentialSearch(arr,target){
for(let i=0; i<item.length; i++){
if(arr[i]===item){
return i
}
}
return -1
}
- 二分搜索
function binarySearch(target,arr,start,end) {
var start = start
var end = end
var mid = parseInt((start+end)/2)
if(target === arr[mid]){
return mid
}else if(target > arr[mid]){
return binarySearch(target,arr,mid+1,end)
}else{
return binarySearch(target,arr,start,mid-1)
}
return -1;
}
网友评论