partia e vjosa osmanit
snippet in c

partia e vjosa osmanit

user9415

// Logarithmic Time using Binary Searchlet arr = [1, 3, 4, 5, 6, 8, 9, 11, 12, 13, 15, 16, 17, 19, 20];

function binarySearch(arr, value) {    
let left = 0,        
	right = arr.length - 1;    
    
    while (left <= right) {      
    
let mid = left + Math.floor((right - left) / 2);

		if (arr[mid] === value) {
        	return mid;        
        } else if (arr[mid] < value) {
        left = mid + 1;        
       	} else {
        	right = mid - 1;        
        }    
    }    
    return -1;
    
}
    
binarySearch(arr, 8); // => 5
binarySearch(arr, 7); // => -1