MathDB
2019 C/CS4: A Search Algorithm

Source:

January 27, 2019
search2019computer sciencealgorithm

Problem Statement

Define a search algorithm called powSearch\texttt{powSearch}. Throughout, assume AA is a 1-indexed sorted array of distinct integers. To search for an integer bb in this array, we search the indices 20,21,2^0,2^1,\ldots until we either reach the end of the array or A[2k]>bA[2^k] > b. If at any point we get A[2k]=bA[2^k] = b we stop and return 2k2^k. Once we have A[2k]>b>A[2k1]A[2^k] > b > A[2^{k-1}], we throw away the first 2k12^{k-1} elements of AA, and recursively search in the same fashion. For example, for an integer which is at position 33 we will search the locations 1,2,4,31, 2, 4, 3.
Define g(x)g(x) to be a function which returns how many (not necessarily distinct) indices we look at when calling powSearch\texttt{powSearch} with an integer bb at position xx in AA. For example, g(3)=4g(3) = 4. If AA has length 6464, find g(1)+g(2)++g(64).g(1) + g(2) + \ldots + g(64).