BINARY SEARCH

import java.io.*; class Bsearch {  public static void main (String args[ ] )throws IOException    { BufferedReader br=new Buffer...

import java.io.*;
class Bsearch
{
 public static void main (String args[ ] )throws IOException
   {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("ENTER ARRAY SIZE");
            int n=Integer.parseInt(br.readLine()); 
            int a[]=new int[5];
            for(int i=0;i
            {
                        System.out.println("ENTER A ["+i+"] ELEMENT");
                        a[i]= Integer.parseInt(br.readLine()); 
            }
            sort s= new sort();  
            s.sorting(a);
            int min=0;
            int max=n-1;
            System.out.println("ENTER ELEMENT TO SEARCH");
            int ele = Integer.parseInt(br.readLine());
            while (min<=max)
            {
                        int mid=(min+max)/2;
                        if(a[mid]==ele)
                        {
System.out.println("ELEMENT FOUND AT POSITIONs" +mid);
                        }
                        else if(a[mid]
                        min=mid+1;
                        else
                        min=mid+1;
            }
  }
}
class sort
{
  public int [] sorting(int a[])
  {
    int n = a.length;
    for(int i=0;i
   {
     for(int j=0;j
    {
      if(a[j]>a[j+1])
      {
         int temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
     }
  }
  return a;
 }
}

Output:

ENTER ARRAY SIZE
5
ENTER ARRAY ELEMENTS
ENTER A[0]ELEMENT
1
ENTER A[1]ELEMENT
2
ENTER A[2]ELEMENT
3
ENTER A[3]ELEMENT
4
ENTER A[4]ELEMENT
5
ENTER ELEMENT TO SEARCH
3
ELEMENT FOUND AT POSITIONs2
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5






Related

Data Structures 1403194319453674822

Post a Comment

emo-but-icon

item