-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearch.c
48 lines (44 loc) · 949 Bytes
/
binarysearch.c
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
/*
Author: JEC21AD041 SACHIN RAJ M
Date : 29/09/2022 Thursday
Exp No : 2
Aim : Implementing Binary Search in Array
*/
#include <stdio.h>
void main()
{
int n,item,temp,mid;
printf("Enter the size of the Array:");
scanf("%d",&n);
int a[n];
printf("Enter the array elements:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to find:");
scanf("%d",&item);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i]>a[j]) //using bubble sort
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int l=0,u=n-1;
while(l<=u){
mid = (l+u)/2;
if(a[mid]==item)
{
printf("Search Successful. Element found in Position %d.\n",(mid+1));
return;
}
else if(a[mid]>item)
u=mid-1;
else
l = mid+1;
}
if(l>u)
printf("Search Unsuccessful. ITEM NOT FOUND.");
}