Searching Algorithms Linear Search & Binary Search - DataStructure - JAVA

Program:
import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    public int flag=0;
    public int lownext=1;
    public int highnext;
    public arraylist(int x)
    {
        a =new int[x];
        n=0;
        highnext=x-1;
    }
   
    public void insert(int p)
    {
        n++;
        a[n]=p;
    }
   
    public void display()
    {
        for(int i=1;i<=n;i++)
            System.out.print(a[i]+" ");
        System.out.println("\n");
    }
   
    public void sort()
    {
        for(int i=1;i<n;i++)
        for(int j=1;j<=n-i;j++)
            if (a[j]>a[j+1])
                swap(j,j+1);
    }
    public void swap(int l,int m)
    {
        int temp=a[l];
        a[l]=a[m];
        a[m]=temp;
    }
    public int bin(int x,int low,int high)
    {
        int mid;
        do
        {
            if(low==high-1)
                flag=1;
            System.out.println(".....Searching between"+a[low]+" & "+a[high] );
            if(x==a[high])
            {
                mid=high;
                System.out.println("\t\tComparing with "+a[mid]+" .....");
                break;
            }
            else
                mid=(low+high)/2;
            System.out.println("\t\tComparing with "+a[mid]+" .....");
            if(x>a[mid])
                low=mid;
            else
                high=mid;
       
        }while(a[mid]!=x&&low<high&&x>=a[low]&&x<=a[high]);
        lownext=low;
        highnext=high;
        if(a[mid]==x)
        {
            System.out.println("\n\t\t.....Found.....");
            return mid;
        }
        else
        {
           
            return 0;
           
        }
    }
    public int linsearch(int low,int high,int key)
    {   
        for(int i=low;i<=high;i++)
        {
            lownext=i+1;
            if(i==high)
                flag=1;
            System.out.println("\t\tComparing with "+a[i]+" .....>");
            if(a[i]==key)
                {
                    System.out.println("\t\t\t\t<..... Yes,found...");
                    return i;
                }
        }
       
        return 0;
    }   
       
}
class search
{
    public static void main(String a[]) throws IOException
    {
        System.out.println("Enter the number of elements:");
        DataInputStream x= new DataInputStream(System.in);
        int s=Integer.parseInt(x.readLine());
        arraylist n=new arraylist(s+1);
        System.out.println("Enter elements:");
        for(int i=0;i<s;i++)
            n.insert(Integer.parseInt(x.readLine()));
        n.display();
        System.out.print("Enter the number to be searched:");
        int w=Integer.parseInt(x.readLine());       
        System.out.println("\n\t Now Enter Your choice:\n\t\t1:Linearsearch\n\t\t\t2:Binarysearch");
        int sw=Integer.parseInt(x.readLine());
        System.out.println(" Searching for"+w);
        int[] f=new int[10];
        int e=0;

       

        if(sw==1)
        {
            while(n.flag==0)
            {
                int pn=n.linsearch(n.lownext,s,w);
                if(pn!=0)
                {
                    f[e]=pn;
                    e++;
                }
           
            }
       
        }
        else if(sw==2)
        {
            n.sort();
            System.out.print("After sorting....\n");
            n.display();
            {
                int pn=n.bin(w,1,s);
                if(pn!=0)
                {
                    f[e]=pn;
                    e++;
                }
            }
        }
        else
        {
            System.out.print("Error....\n");
        }
        if(e>0)
        {
            System.out.print("The number is present at position: ");
            for(int i=0;i<e;i++)
                System.out.print(f[i]+" ");
        }
        else
            System.out.println("Sorry...The number is not present....");

    }
}


Output:
Enter elements:
9
8
7
5
4
2
1
4
6
8
9 8 7 5 4 2 1 4 6 8

Enter the number to be searched:4

     Now Enter Your choice:
        1:Linearsearch
            2:Binarysearch
1
 Searching for4
        Comparing with 9 .....>
        Comparing with 8 .....>
        Comparing with 7 .....>
        Comparing with 5 .....>
        Comparing with 4 .....>
                <..... Yes,found...
        Comparing with 2 .....>
        Comparing with 1 .....>
        Comparing with 4 .....>
                <..... Yes,found...
        Comparing with 6 .....>
        Comparing with 8 .....>
The number is present at position: 5 8

students@ccflab-desktop:~/D$ java search
Enter the number of elements:
5
Enter elements:
5
4
3
2
1
5 4 3 2 1

Enter the number to be searched:3

     Now Enter Your choice:
        1:Linearsearch
            2:Binarysearch
2
 Searching for3
After sorting....
1 2 3 4 5

.....Searching between1 & 5
        Comparing with 3 .....

        .....Found.....
The number is present at position: 3

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...