-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathrecursiveBinarySearch.c
60 lines (57 loc) · 1.24 KB
/
recursiveBinarySearch.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
#include <stdio.h>
/**
* this function contains the logic for recursive binary search
*
* @param array
* @param leftIndex
* @param rightIndex
* @param key
*
* @return int
**/
int binarySearch(int array[], int leftIndex, int rightIndex, int key)
{
if(leftIndex <= rightIndex)
{
int midIndex = (leftIndex + rightIndex) / 2;
if(array[midIndex] == key)
{
return midIndex;
}
if(array[midIndex] < key)
{
int ans = binarySearch(array, midIndex+1, rightIndex, key);
return ans;
}
else
{
int ans = binarySearch(array, leftIndex, midIndex-1, key);
return ans;
}
}
return -1;
}
int main()
{
printf("Enter the size of the array\n");
int size;
scanf("%d", &size);
int array[size];
printf("Enter the elements of the array in sorted order\n");
for(register int i=0; i<size; i++)
{
scanf("%d",&array[i]);
}
printf("Enter the element to find\n");
int key;
scanf("%d", &key);
int index = binarySearch(array, 0, size, key);
if(index==-1)
{
printf("Not found!!!\n");
}
else
{
printf("Found at %d index\n",index);
}
}