How to implement quaternary search?
<p>I know this algorithm is no better than binary search, but it's an interesting thought experiment for understanding recursion.  I haven't been able to get very far on this though(way too much recursion for my brain to handle.).  Anyone know how to actually implement this?</p>

<p>I have the following, which is nowhere close to correct.  I really don't even know how to approach this, it's like the search version of inception.</p>

<pre><code>def srb(list,key,q1,q2,q3,q4):
    mid1 = (q1+q2)//2
    mid2 = (q3+q4)//2
    if mid1 &lt; list[0] or mid1 &gt; list[-1] or key &lt; list[0] or key &gt; list[-1]:
        return False
    if mid2 &lt; list[0] or mid2 &gt; list[-1] or key &lt; list[0] or key &gt; list[-1]:
        return False
    elif key == mid1 or key == mid2:
        return True
    elif key &gt; mid1 and key &lt; q3:
        return srb(list,key,mid1+1,q2)
    elif key &lt; mid1:
        return srb(list,key,q1,mid1-1)
    elif key &gt; q3 and key &lt; mid2:
        return srb(list,key,q3+1,mid2)
    else:
        return srb(list,key,mid2+1,q4)
</code></pre>

<p>Start with a correct implementation of binary search.  Then replace each recursive call with an inline version of the whole search (minus any initialization), i.e expand the binary search code in place as if it were a macro expansion [change variable names as necessary].  I believe if you do it right you will then have a quarternary search procedure.</p>

python algorithm search binary-search