Are you sure this will return the values in order?  Seems like this algorithm relies on the bins being ordered (so small ones come first, right?).