-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search.c
More file actions
75 lines (72 loc) · 1.76 KB
/
Copy pathbinary_search.c
File metadata and controls
75 lines (72 loc) · 1.76 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
int binarySearch(int arr[], int size, int element)
{
int low, mid, high;
low = 0;
high = size-1;
// start searching element
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] == element){
return mid;
}
if (arr[mid]<element)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int arr[] = {1,3 ,5,6,8,9,10,22,33,44,66};
int size = sizeof(arr)/sizeof(int);
int element = 5;
int search = binarySearch(arr,size,element);
printf("The element %d was found at index %d\n",element,search);
return 0;
}
// #include<stdio.h>
// int binarySearch(int[], int, int, int); // function declaration
// void main ()
// {
// int arr[10] = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100}; // binary element need to be sorted
// int number, location=0;
// printf("Enter the item which you want to search : \n");
// scanf("%d",&number); // number want to search
// location = binarySearch(arr, 0, 9, number);
// if(location != 0)
// {
// printf("Item found at location = %d",location);
// }
// else
// {
// printf("Item not found");
// }
// }
// int binarySearch(int a[], int beg, int end, int number)
// {
// int mid;
// if(end >= beg)
// {
// mid = (beg + end)/2; // logic for getting medium
// if(a[mid] == number)
// {
// return mid+1;
// }
// else if(a[mid] < number)
// {
// return binarySearch(a,mid+1,end,number);
// }
// else
// {
// return binarySearch(a,beg,mid-1,number);
// }
// }
// return 0;
// }