File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ void main ()
3
+ {
4
+ int mid ,lb ,ub ,i ,n ,key ;
5
+ printf (" Enter The Size of 1D Array\n" );
6
+ scanf (" %d" , & n );
7
+ int arr [n ];
8
+ printf (" Enter The Elements into the Array: \n" );
9
+ for (i = 0 ;i < n ;i ++ )
10
+ scanf ("%d" ,& arr [i ]);
11
+ printf ("\n\n The Given Array Is:\n \n" );
12
+ for (i = 0 ;i < n ;i ++ )
13
+ printf ("%d " ,arr [i ]);
14
+ printf ("\n Enter the number to be searched:" );
15
+ scanf ("%d" , & key );
16
+ lb = 0 ;
17
+ ub = n - 1 ;
18
+ mid = (lb + ub )/2 ;
19
+ while (lb <=ub )
20
+ {
21
+ if (key == arr [mid ])
22
+ {
23
+ printf ("\n %d found at %d" ,key ,(mid + 1 ));
24
+ break ;
25
+ }
26
+ else if (key < arr [mid ])
27
+ ub = mid - 1 ;
28
+ else if (key > arr [mid ])
29
+ lb = mid + 1 ;
30
+ else
31
+ {
32
+ printf ("\n %d not found in the array" , key );
33
+ break ;
34
+ }
35
+ mid = (lb + ub )/2 ;
36
+ }
37
+ }
38
+
You can’t perform that action at this time.
0 commit comments