forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exponential_Search.cs
59 lines (45 loc) · 1.26 KB
/
Exponential_Search.cs
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
using System;
class exponentialSearch {
static int exponentialSearch(int []arr, int n, int x)
{
if (arr[0] == x)
return 0;
// Find range for binary search by repeated doubling
int i = 1;
while (i < n && arr[i] <= x)
i = i * 2;
// Call binary search for the found range.
return binSearch(arr, i/2, Math.Min(i, n), x);
}
static int binSearch(int []arr, int left, int right, int x)
{
if (right >= left)
{
int mid = left + (right - left) / 2;
// If the element is present at the middle itself
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binSearch(arr, left, mid - 1, x);
// Else the element can only be present in right subarray
return binSearch(arr, mid + 1, right, x);
}
return -1;
}
// Driver code
public static void Main()
{
// Sample Input
int []input = {2, 3, 14, 20, 40};
int n = input.Length;
int x = 20;
int result = exponentialSearch(input, n, x);
if (result == -1)
Console.Write("Element is not present in array");
else
Console.Write("index "+ result);
}
}
/* Sample Output :
index 3
*/