خوارزمية البحث

البحث الثنائي

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

        if(element == arr[mid]){
            return mid;
        }

        if(element > arr[mid]){
            left = mid + 1;
        }else{
            right = mid - 1;
        }
    }
  
    return -1;
}