diff --git a/Contributors.html b/Contributors.html index 0c7036042a..c8b9a682cf 100644 --- a/Contributors.html +++ b/Contributors.html @@ -101,8 +101,6 @@

Contributors

- - Anmol Agarwal Amitava Mitra @@ -154,6 +152,7 @@

Contributors

Ido Evergreen Zaharia Iulian Haryfun +Alok Tripathi Quentin Troemner Vikramaditya Singh Saxena diff --git a/Program's_Contributed_By_Contributors/ExponentialSearching.cpp b/Program's_Contributed_By_Contributors/ExponentialSearching.cpp new file mode 100644 index 0000000000..65092e078a --- /dev/null +++ b/Program's_Contributed_By_Contributors/ExponentialSearching.cpp @@ -0,0 +1,48 @@ +// C++ program to find an element x in a +// sorted array using Exponential search. +#include +using namespace std; + +int binarySearch(int arr[], int, int, int); + +int exponentialSearch(int arr[], int n, int x) +{ + if (arr[0] == x) + return 0; + int i = 1; + while (i < n && arr[i] <= x) + i = i*2; + + return binarySearch(arr, i/2, min(i, n), x); +} + +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) + { + int mid = l + (r - l)/2; + + if (arr[mid] == x) + return mid; + + if (arr[mid] > x) + return binarySearch(arr, l, mid-1, x); + + return binarySearch(arr, mid+1, r, x); + } + + return -1; +} + +// Driver code +int main(void) +{ +int arr[] = {2, 3, 4, 10, 40}; +int n = sizeof(arr)/ sizeof(arr[0]); +int x = 10; +int result = exponentialSearch(arr, n, x); +(result == -1)? printf("Element is not present in array") + : printf("Element is present at index %d", + result); +return 0; +}