BINARY SEARCH

#include<stdio.h>
#include<conio.h>
int binary(int n,int item[],int key)
{
     int low,hi,mid;
     low=0;
     hi=n-1;
     while(low<=hi)
     {
           mid=(low+hi)/2;
           if(key==item[mid])
           {
                return(mid);
           }
           if(key<item[mid])
           {
                hi=mid-1;
           }
           else
           {
                low=mid+1;
           }
     }
     return(-1);
}

void main()
{
     int item[10],n=10,i,j,temp;
     char ch='y';
     clrscr();
     printf("\nEnter The Element For The Binary Search\n\n");
     for(i=0;i<n;i++)
     {
           printf("\tEnter The Value at %d : ",i);
           scanf("%d",&item[i]);
     }
     for(i=0;i<n-1;i++)
     {
           for(j=i+1;j<n;j++)
           {
                if(item[i]>item[j])
                {
                     temp=item[i];
                     item[i]=item[j];
                     item[j]=temp;
                }

           }
     }
     while(ch=='y' || ch=='Y')
     {
           clrscr();
           printf("\n\t--------------------------");
           printf("\n\t-:\t\b\bSorted Element\t:-");
           printf("\n\t--------------------------");
           for(i=0;i<n;i++)
           {
                printf("\n\t\t\b\bValue at %d : %d",i,item[i]);
           }
           printf("\n\t--------------------------");
           printf("\n\n\tEnter the No To Search : ");
           scanf("%d",&i);
           i=binary(n,item,i);
           if(i==-1)
           {
                printf("\n\tSEARCH IS SUCCESSFULLY FAILED");
           }
           else
           {
                printf("\n\tSEARCH IS SUCCESSFUL AT : %d",i);
           }
           printf("\n\n\tDo You Want To Continue (Y/N) : ");
           ch=getch();
     }
}

OUTPUT :

Enter The Element For The Binary Search

        Enter The Value at 0 : 10
        Enter The Value at 1 : 20
        Enter The Value at 2 : 74
        Enter The Value at 3 : 65
        Enter The Value at 4 : 01
        Enter The Value at 5 : 54
        Enter The Value at 6 : 63
        Enter The Value at 7 : 96
        Enter The Value at 8 : 98
        Enter The Value at 9 : 78
        -----------------------------
        -:    Sorted Element    :-
        -----------------------------
              Value at 0 : 1
              Value at 1 : 10
              Value at 2 : 20
              Value at 3 : 54
              Value at 4 : 63
              Value at 5 : 65
              Value at 6 : 74
              Value at 7 : 78
              Value at 8 : 96
              Value at 9 : 98
        -----------------------------

        Enter the No To Search : 12

        SEARCH IS SUCCESSFULLY FAILED

        Do You Want To Continue (Y/N) : Y

        Enter the No To Search : 65

        SEARCH IS SUCCESSFUL AT : 5


        Do You Want To Continue (Y/N) : N

No comments:

Post a Comment

Python Code Loop Program-1

 #Chapter -7 Qno - 2 num=4; while(num<10):     print("Number is %d"%num);     num=num+1; Output Number is 4 Number is 5 Number ...