Home > Blockchain >  Binary Search function is not getting implemented [duplicate]
Binary Search function is not getting implemented [duplicate]

Time:10-03

All other parts of the code is getting executed but the problem occurs when its comes to the execution of binary search.The algorithm i have done is right but its always giving me the opposite answer.I don't get it whats happening in the code. Even when I execute the program and tell the code to search for a element that is present in the array . The linear search gives me the correct answer that is the element is present but when it goes into the binary search it says that the element is not present.

import java.util.*;
public class VOperations {
 
    int n,first,last;
    
     
    
    void intializer(int Arr[]){
        n=Arr.length;
        

     }

     void show(int Arr[])
     {
         
         for (int i=0; i<n;   i)
             System.out.print(Arr[i]   " ");
         System.out.println();
     }
    
     void linearSearch(int s,int Arr[]){
        int c =0;
        for(int i=0;i<n;i  ){
            if(Arr[i]==s){
                c  ;
            }
        }
        if(c==1)
        System.out.println("Element found");
        else
        System.out.println("Not found");

    }
   
   
    int binSearch(int Arr[], int x)
    {
        int l = 0, r = Arr.length - 1;
        while (l <= r) {
            int m = l   (r - l) / 2;
  
            
            if (Arr[m] == x)
                return m;
  
           
            if (Arr[m] < x)
                l = m   1;
  
           
            else
                r = m - 1;
        }
  
       
        return -1;
    }

      void bubbleSort(int Arr[])
      {
          int n = Arr.length;
          for (int i = 0; i < n-1; i  )
              for (int j = 0; j < n-i-1; j  )
                  if (Arr[j] > Arr[j 1])
                  {
                      
                      int temp = Arr[j];
                      Arr[j] = Arr[j 1];
                      Arr[j 1] = temp;
                  }
         
          show(Arr);        
      }
}
class Test3{
    public static void main(String args[]){
        VOperations ob=new VOperations();
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter the number you want to search :");
        int x = sc.nextInt();
        int Arr[] = { 11,6,77,8,5,44,6,9,442,86,73,49,68,82 };


        ob.intializer(Arr);
        ob.show(Arr);
        int result = ob.binSearch(Arr, x);
        if (result == -1)
            System.out.println("Element not present");
        else
            System.out.println("Element found at "
                                 "index "   result);
    
        ob.linearSearch(x, Arr);
        ob.bubbleSort(Arr);
        sc.close();
    }
}

CodePudding user response:

Binary search applies only to sorted inputs. Your array is not initially in order, and you have not sorted it yet when you attempt the binary search.

Linear search does not have the same constraint.

  • Related