-
Notifications
You must be signed in to change notification settings - Fork 0
/
seaarching-binarySearch.c
67 lines (59 loc) · 1.18 KB
/
seaarching-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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#define s 1000
// Function to Swap 2 value Address
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
// Function for Bubble Sort Operations
void bubbleSort(int a[], int n){
int i, j;
for(i=0; i<n-1; ++i){
for(j=0; j<n-i-1; ++j){
if(a[j]>a[j+1]){
swap(&a[j], &a[j+1]);
}
}
}
}
// Function for binary search operation
void binarySearch(int a[], int n, int x){
int i, count=0;
bubbleSort(a,n);
for(i=0; i<n; i++){
int beg = i, end = n-1, mid = (beg+end)/2;
while(beg<=end)
{
if(a[mid] > x){
end = mid - 1;
mid = (beg+end)/2;
}
else{
beg = mid + 1;
mid = (beg+end)/2;
}
if(a[mid] == x){
count++;
}
}
}
if(count>0){
printf("Value Matched\n");
}
else{
printf("Value Not Found\n");
}
}
int main()
{
int n, a[s];
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%d", &a[i]);
}
int x;
scanf("%d", &x);
binarySearch(a,n,x);
}