Implement functionality in PHP?

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-03-15T05:19:05Z Indexed on 2010/03/15 5:19 UTC
Read the original article Hit count: 340

How can we Implement Bisect Python functionality in PHP

Implement function bisect_left($arr, $item);

as a pure-PHP routine to do a binary-bisection search for the position at which to insert $item into $list, maintaining the sort order therein.

Assumptions:

  1. Assume that $arr is already sorted by whatever comparisons would be yielded by the stock PHP < operator, and that it's indexed on ints.
  2. The function should return an int, representing the index within the array at which $item would be inserted to maintain the order of the array. The returned index should be below any elements in $arr equal to $item, i.e., the insertion index should be "to the left" of anything equal to $item.
  3. Search routine should not be linear! That is, it should honor the name, and should attempt to find it by iteratively bisecting the list and comparing only around the midpoint.

© Stack Overflow or respective owner

Related posts about interview-questions

Related posts about php