-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarysearch.c
More file actions
55 lines (54 loc) · 1.2 KB
/
Copy pathBinarysearch.c
File metadata and controls
55 lines (54 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
#include <stdio.h>
int binarysearch(int array[], int size, int number)
{
int least = 0;
int max = size - 1;
while (least <= max)
{
int half = least + (max - least) / 2;
if (array[half] == number)
{
return half;
}
if (array[half] < number)
{
least = half + 1;
}
if (array[half] > number)
{
max = half - 1;
}
}
return 2;
}
int main()
{
int array[10];
int number, result;
printf("Populate your array\n");
for (int i = 0; i < 10; i++)
{
scanf("%d", &array[i]);
}
printf("Enter your value to search\n");
scanf("%d", &number);
for (int i = 0; i < 9; i++)
{
for (int j = i + 1; j < 10; j++) //For binary search it's compulsoray to sort
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
result = binarysearch(array, 10, number);
if (result !=2)
{
printf("Your number is found at index number %d in the sorted array", result);
}
else
printf("Not found\n");
}