Divide And Conquer

anonymous·2021년 5월 7일
0

알고리즘

목록 보기
6/12
/*
Q:
Given a sorted array of integers, write a function called search, that accepts a value and returns the index where the value passed to the function is located. If the value is not found, return -1
*/
//binary search
function searchB(array, val){
	let min=0; //Init left
	let max=array.length - 1; //Init right

	while(min <= max){
	    let middle = Math.floor((min + max) / 2); //INIT Middle
	    let currentElement = array[middle];

	    if(array[middle] < val){ //Check input search value to middle. 
	        min = middle + 1; //Init new min
	    }else if (array[middle] > val){
	        max = middle - 1; //Init new max
	    }else{
	        return middle;
	    }
	}
	return -1;

}


console.log(searchB([1,2,3,4,5,6], 8));
console.log(searchB([1,2,3,4,8,6], 8));
profile
기술블로거입니다

0개의 댓글