[Assorted-commits] SF.net SVN: assorted:[976] sandbox/trunk/src/py/bisearch.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-09-29 23:57:32
|
Revision: 976 http://assorted.svn.sourceforge.net/assorted/?rev=976&view=rev Author: yangzhang Date: 2008-09-29 23:57:16 +0000 (Mon, 29 Sep 2008) Log Message: ----------- added binary search Added Paths: ----------- sandbox/trunk/src/py/bisearch.py Added: sandbox/trunk/src/py/bisearch.py =================================================================== --- sandbox/trunk/src/py/bisearch.py (rev 0) +++ sandbox/trunk/src/py/bisearch.py 2008-09-29 23:57:16 UTC (rev 976) @@ -0,0 +1,14 @@ +#!/usr/bin/env python + +def bisect(xs, q): + if len(xs) == 1: + return 0 + else: + midpt = xs[len(xs)/2] + print midpt + if q < midpt: + return bisect(xs[:len(xs)/2], q) + else: + return len(xs)/2 + bisect(xs[len(xs)/2:], q) + +print bisect([11,12,14,15,16,19,21,22,25], 22) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |