TheAlgorithms-PHP/Searches/LowerBound.php

38 lines
913 B
PHP

<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../Utils/ArrayHelpers.php';
/**
* Lower Bound of an element is minimum index that an element would be placed
* if it is added into that sorted array
*
* [C++ Lower Bound](http://www.cplusplus.com/reference/algorithm/lower_bound/)
*
* It is assumed that an integer array is provided
* and the second parameter is also an integer
*
* @param array $arr of sorted integers
* @param integer $elem whose lower bound is to be found
*
* @return int the index of lower bound of the given element
*/
function lowerBound(array $arr, int $elem)
{
isSortedAscendingInts($arr);
$hi = count($arr);
$lo = 0;
while ($lo < $hi) {
$mid = $lo + floor(($hi - $lo) / 2);
if ($arr[$mid] < $elem) {
$lo = $mid + 1;
} else {
$hi = $mid;
}
}
return $lo;
}