/* Exponential search involves two steps, first to find the range within which the element is found and searching inside that range using binary search. */ #include #include int binary_search(std::vector arr, int item, int low, int high){ int mid; while(low <= high){ mid = low + (high - low)/2; if(arr[mid] == item){ return mid; } else if(arr[mid] < item){ low = mid + 1; } else{ high = mid - 1;} } return -1; } int exponential_search(std::vector arr, int item){ int k = 1; int size = arr.size() - 1; while(k < size && arr[k] <= item){ k *= 2; } return binary_search(arr, item, k/2, std::min(k, size)); } int main(){ std::vector arr; for(int i=1; i<30; i+=2){ arr.push_back(i); } for(int i=0; i