News
  • Submission of Theory Assignments 6,7,8 of OS on 1st April
  • Case Study Presentation of OS on 4st April
  • CN and PNS Journal Submission on 6th April

DS (RM) Program - Binary Search


DS (RM) Program - Binary Search









 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream.h>                                                      
#include<conio.h>                                                         
#include<stdlib.h>                                                        
#include<math.h>                                                          
#define SIZE 25                                                           
                                                                          
int n=0;                                                                  
int bnrysear(int[], int, int, int*);                                      
                                                                          
void main()                                                               
{                                                                         
        int i,flag,e,key,*locn,option;                                    
        int L[SIZE];                                                      
                                                                          
        clrscr();                                                         
                                                                          
        while(1)                                                          
        {                                                                 
                cout<<"\n1. input unordered list : \n";                   
                cout<<"2. Search for key using binary search : \n";   
                cout<<"3. Quit \n";                                       
           cout<<"\n Enter Option : ";                     
       cin>>option;                                    
                                                    
           switch(option)                                  
           {                                               
      case 1:                                         
              cout<<"\n\nno.of Elements ? ";          
                    cin>>n;                                 
              cout<<"\nInput list of elements : ";    
                   for(i=0; i<n; i++)                      
                   {                                       
                      L[i] = 0;                       
                             cin>>L[i];                      
              }     
break;                                  
                                                    
      case 2:                                         
              cout<<"\n\nEnter Search key !\n";       
            cin>>key;                               
            flag=bnrysear(L,key,e,locn);            
                       if(flag==1)                             
                         {                                        
                                cout<<"\n Data is Found ...\n";  
                                cout<<"Location is : ";          
                                cout<<*locn+1;                   
                         }                                        
                         else                                     
                                cout<<"\n Data is Not Found ...";
                                                                 
                         break;                                   
                case 3:                                          
                        exit(0);                                 
                                                                 
                default:                                         
                        cout<<"Illegal option ...\n";            
                                                                 
                }                                                
        }                                                        
                                                                 
        getch();                                                 
}

int bnrysear(int l[], int target, int end, int *locn)
{
        int mid, first, last;
        first=0;
        last=end;

        while(first<=last)
        {
                mid = ceil((first + last)/2);

                if((target < l[mid]))
                        first = mid + 1;
                else
                        if(target > l[mid])
                                last = mid - 1;
                        else
                                break;
        }

        *locn = mid;
         return (target==l[mid]);
}
Output:

1. input unordered list :
2. Search for key using binary search :
3. Quit

 Enter Option : 1


no.of Elements ? 4

Input list of elements : 12
45
3
87

1. input unordered list :
2. Search for key using binary search :
3. Quit

 Enter Option : 2


Enter Search key !
22

 Data is Not Found ...
1. input unordered list :
2. Search for key using binary search :
3. Quit

 Enter Option : 3
© Copyright NMITD MCA Notes Published.. Blogger Templates
Back To Top