-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary_search_udf.c
62 lines (49 loc) · 1.42 KB
/
binary_search_udf.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
61
62
#include <stdio.h>
int find(int[], int, int);
int binarySearch(int[], int, int, int);
int Binary_Search(int arr[], int n, int key)
{
int pivot = find(arr, 0, n - 1);
if (pivot == -1)
return binary_Search(arr, 0, n - 1, key);
if (arr[pivot] == key)
return pivot;
if (arr[0] <= key)
return binary_Search(arr, 0, pivot - 1, key);
return binary_Search(arr, pivot + 1, n - 1, key);
}
int find(int arr[], int low, int high)
{
if (high < low)
return -1;
if (high == low)
return low;
int mid = (low + high) / 2;
if (mid < high && arr[mid] > arr[mid + 1])
return mid;
if (mid > low && arr[mid] < arr[mid - 1])
return (mid - 1);
if (arr[low] >= arr[mid])
return find(arr, low, mid - 1);
return find(arr, mid + 1, high);
}
int binary_Search(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binary_Search(arr, (mid + 1), high, key);
return binary_Search(arr, low, (mid - 1), key);
}
int main()
{
int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 };
int n = sizeof(arr1) / sizeof(arr1[0]);
int key = 3;
printf("Index of the element is : %d",
Binary_Search(arr1, n, key));
return 0;
}