-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Linear Search.c
More file actions
63 lines (50 loc) · 1.2 KB
/
Binary Linear Search.c
File metadata and controls
63 lines (50 loc) · 1.2 KB
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
/*14. Write a program to search an element from the array using following Searching
Techniques:
a. Linear Search
b. Binary Search*/
#include <stdio.h>
int binarySearch(int array[], int x, int low, int high)
{
// Repeat until the pointers low and high meet each other
while (low <= high)
{
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int linearSearch(int array[], int search, int n)
{
for(int i = 0; i < n; i++)
{
if(array[i] == search)
return i;
}
return -1;
}
int main(void)
{
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
//binary search
int result = binarySearch(array, x, 0, n - 1);
printf("\nResult Using binarySearch: ");
if (result == -1)
printf("\nElement not found");
else
printf("\nElement is found at index %d", result);
//linear search
int result = linearSearch(array, x, n);
printf("\nResult using linearSearch: ");
if(result == -1)
printf("\nElement not present");
else
printf("\nElement found at %d", result);
return 0;
}