선형검색
function linearSearch(arr,val){
for(let i=0; i <arr.length; i ++){
if (arr[i] === val){
return i
}
}
return -1;
}
linearSearch([32,56,1,2],1)
이진 검색(Binary Search)
function binarySearch(arr,elem){
let start = 0;
let end = arr.length -1;
let middle = Math.floor((start + end) / 2);
console.log(start,end,middle)
while(arr[middle] !== elem && (start + end)/ 2){
if(elem < arr[middle]) end = middle -1;
else start = middle +1;
middle = Math.floor((start + end) / 2);
}
return arr[middle] === elem ? middle : -1;
}
binarySearch([2,5,6,8,10,22,35,44],10)