-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ4
More file actions
30 lines (29 loc) · 619 Bytes
/
Q4
File metadata and controls
30 lines (29 loc) · 619 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
Solve the bug in this program of binary search
#include <stdio.h>
int Search(int arr[], int lo, int hi, int item)
{
int mid;
if (lo > hi)
return -1;
mid = (lo + hi) / 2;
if (arr[mid] == item)
return mid;
else if (arr[mid] > item)
Search(arr, mid-1, hi, item);
else if (arr[mid] < item)
Search(arr, lo, mid+1, item);
}
int main()
{
int arr[] = { 10, 21, 23, 46, 75 };
int index = 0;
int item = 0;
printf("Enter item to search: ");
scanf("%d", &item);
index = Search(arr, 0, 5, item);
if (index == -1)
printf("Item not found in array\n");
else
printf("Item found at index %d\n", index);
return 0;
}