-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
61 lines (59 loc) · 959 Bytes
/
binary_search.cpp
File metadata and controls
61 lines (59 loc) · 959 Bytes
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
#include<stdio.h>
void bubble(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int idk=0;idk<n;idk++)
{
printf("index %d:[%d]",idk,a[idk]);
}
}
int binary(int arr[],int low,int high)
{
printf("\nenter the array element want to serch:");
int hold=0;
scanf("%d",&hold);
while(low<=high)
{
int mid= (low+high)/2;
if(arr[mid]==hold)
{
return mid;
}
else if(arr[mid]<hold)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
return -1;
}
int main()
{
printf("enter the array size:");
int n;
scanf("%d",&n);
int arr[n];
printf("enter the array element---\n");
for(int idk=0;idk<n;idk++)
{
printf("enter the array element for index for arr[%d]",idk);
scanf("%d",&arr[idk]);
}
bubble(arr,n);
int re=binary(arr,0,n);
(re==-1)?printf("Array element not found"):printf("Array element found:%d",re);
}