Showing posts with label Binarysearch. Show all posts
Showing posts with label Binarysearch. Show all posts

Binary Search - LISP

Program:
;;BINARYSEARCH
(defun binsearch(n)
    (format t "~&<<BINARYSEARCH>>~&")
    (bsreadn n)
    (bssortn n)
    (format t "~&Enter the number to be searched:")
    (setf p (read))
    (setf flag 1)
    (setf first 0)
    (setf last (- n 1))
    (dotimes (x n t)
        (setf mid (floor (+ first last) 2))
        (if (= (aref arr mid) p)(setf flag 0))
        (cond
            ( (>(aref arr mid) p) (setf last (- mid 1)))
            ( (<(aref arr mid) p) (setf first (+ mid 1)))
        )
    )
    (if (= flag 1)
        (format t "Number Not found..."))
    (if (= flag 0)
        (format t "Number found..."))
)
(defun bsreadn(n)
    (setf arr (make-array n))
    (format t "Enter the ~d numbers:" n)
    (dotimes (x n t)
        (setf (aref arr x) (read))
    )
)
                        ;;http://www.2k8618.blogspot.com/
(defun bssortn(n)
    (do (( i 1 (+ i 1))) ((= i n))
        (do ((j 0 (+ j 1))) ((= j (- n 1)))
            (if (> (aref arr j) (aref arr (+ j 1)))
                (bswap j (+ j 1))
            )
        )
    )
    (format t "Sorting....")
    (bprintn n)
)
(defun bprintn(n)
    (dotimes(x n t)
        (print (aref arr x))
    )
)
(defun bswap(x y)
    (setf temp (aref arr x))
    (setf (aref arr x) (aref arr y))
    (setf (aref arr y) temp)
)

Output:
Break 2 [3]> (load 'bin.lsp)
;;  Loading file bin.lsp ...
;;  Loaded file bin.lsp
T
Break 2 [3]> (binsearch 5)
<<BINARYSEARCH>>
Enter the 5 numbers:5
4
3
2
1
Sorting....
1
2
3
4
5
Enter the number to be searched:3
Number found...
NIL
Break 2 [3]> (binsearch 5)
<<BINARYSEARCH>>
Enter the 5 numbers:1
4
2
3
5
Sorting....
1
2
3
4
5
Enter the number to be searched:6
Number Not found...
NIL
Break 2 [3]>

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
Related Posts Plugin for WordPress, Blogger...