/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package package11;

/**
 *
 * @author Student
 */

import java.util.*;
 class BSearch
{
    private int x[],n;
    public BSearch(int n)
    {
        this.n=n;
        x=new int[n];
        System.out.println("Enter Data");
        Scanner src=new Scanner(System.in);
        for(int i=0;i<n;i++)
            x[i]=src.nextInt();
    }
    public boolean bSearch(int low,int high,int ele)
    {
        while(low<=high)
        {
            int mid=(low+high)/2;
            if(x[mid]==ele)
                return true;
            if(ele<x[mid])
                high=mid-1;
            else low=mid+1;
        }
        return false;
    }
    public int getNum()
    {
        return n;
    }
    public void bubbleSort()
    {
        for(int i=0;i<n-1;i++)
            for(int j=0;j<n-1;j++)
                if(x[j]>x[j+1])
                {
                    int t=x[j];
                    x[j]=x[j+1];
                    x[j+1]=t;
                }
    }
    public void display()
    {
        System.out.println("The Sorted elements are : ");
        for(int i=0;i<n;i++)
            System.out.print(x[i]+" ");
        System.out.println();
    }
}
class BSearchExp
{
    public static void main(String args[])
    {
        Scanner src=new Scanner(System.in);
        System.out.println("Enter the number of elements");
        int noOfEle=src.nextInt();
        BSearch obj=new BSearch(noOfEle);
        obj.bubbleSort();
        obj.display();
        int c;
        do
        {
            System.out.println("Do you want to continue searching? 1.Yes 2.No");
            c=src.nextInt();
            switch(c)
            {
                case 1:
                    System.out.println("Enter the element to be searched");
                    int ele=src.nextInt();
                    if(obj.bSearch(0, obj.getNum()-1, ele))
                          System.out.println(ele+" is Found");
                    else System.out.println(ele+" not Found");

                break;
                case 2:
                    System.exit(1);
                    break;

                default:System.out.println("Invalid Choice");
            }

        }while(c==1);
    }
}
output
Enter the number of elements
5
Enter Data
74
89
12
56
45
The Sorted elements are : 
12 45 56 74 89 
Do you want to continue searching? 1.Yes 2.No
1
Enter the element to be searched
100
100 not Found
Do you want to continue searching? 1.Yes 2.No
1
Enter the element to be searched
74
74 is Found
Do you want to continue searching? 1.Yes 2.No
2
