Ad
Code
Diff
  • function binarySearch(numbers, element) {
    //time complexity is log(n) it excecutes very fast than linear search  n=arraylength;
      
    return search(numbers,element,0,numbers.length-1)}
    
    function search(numbers,element,left,right){
      if(left>right) return -1;
      
      let pivot =Math.floor((left+right)/2)
      
      if(numbers[pivot]===element) return pivot
      
       if(numbers[pivot]>element) return search(numbers,element,left,pivot-1)
      
        return search(numbers,element,pivot+1,right)
      
        
    }
    • function binarySearch(numbers, element) {
    • //time complexity is log(n) it excecutes very fast than linear search n=arraylength;
    • let left = 0;
    • let right = numbers.length - 1;
    • while(left <= right) {
    • let pivot = Math.floor((left + right) / 2);
    • return search(numbers,element,0,numbers.length-1)}
    • function search(numbers,element,left,right){
    • if(left>right) return -1;
    • let pivot =Math.floor((left+right)/2)
    • if(numbers[pivot]===element) return pivot
    • if(numbers[pivot]>element) return search(numbers,element,left,pivot-1)
    • return search(numbers,element,pivot+1,right)
    • if(numbers[pivot]==element){
    • return pivot;
    • }
    • else if(numbers[pivot]>element){
    • right=pivot-1;
    • }
    • else{
    • left=pivot+1;
    • }
    • }
    • return -1;
    • }
Code
Diff
  • function binarySearch(numbers, element) {
    //time complexity is log(n) it excecutes very fast than linear search  n=arraylength;
      let left = 0;
      let right = numbers.length - 1;
      
      while(left <= right) {
        let pivot = Math.floor((left + right) / 2);
        
        if(numbers[pivot]==element){
          return pivot;
        }
        else if(numbers[pivot]>element){
          right=pivot-1;
        }
        else{
          left=pivot+1;
        }
       }
      return -1;
    }
    • function binarySearch(numbers, element) {
    • //time complexity is log(n) it excecutes very fast than linear search n=arraylength;
    • let left = 0;
    • let right = numbers.length - 1;
    • while(left <= right) {
    • let pivot = Math.floor((left + right) / 2);
    • if(numbers[pivot]==element){
    • return pivot;
    • }
    • else if(numbers[pivot]>element){
    • right=pivot-1;
    • }
    • else{
    • left=pivot+1;
    • }
    • }
    • return -1;
    • }
Code
Diff
  • function binarySearch(numbers, element) {
    
      let left = 0;
      let right = numbers.length - 1;
      
      while(left <= right) {
        let pivot = Math.floor((left + right) / 2);
        
        if(numbers[pivot]==element){
          return pivot;
        }
        else if(numbers[pivot]>element){
          right=pivot-1;
        }
        else{
          left=pivot+1;
        }
       }
      return -1;
    }
    • function binarySearch(numbers, element) {
    • let left = 0;
    • let right = numbers.length - 1;
    • let pivot = Math.floor((left + right) / 2);
    • while(left < pivot && pivot < right) {
    • if(element < numbers[pivot]) {
    • right = pivot;
    • } else {
    • left = pivot;
    • while(left <= right) {
    • let pivot = Math.floor((left + right) / 2);
    • if(numbers[pivot]==element){
    • return pivot;
    • }
    • pivot = Math.floor((left + right) / 2);
    • }
    • if(element === numbers[pivot]) {
    • return pivot;
    • }
    • else if(numbers[pivot]>element){
    • right=pivot-1;
    • }
    • else{
    • left=pivot+1;
    • }
    • }
    • return -1;
    • }