/**
 * Searching maximum subarray inside given array
 *
 * @package maximum_subarray
 * @author Andrey Babaev <*****@*****.**>
 */
require_once '../common/functions.php';
define('PHP_INT_MIN', ~PHP_INT_MAX);
// Prepare test array
$a = array(13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7);
// Print array before sorting
array_before($a);
// Sort array
$a = maximum_subarray($a);
//Print array after sorting
array_after($a);
measure_exec_time_rand('maximum_subarray', 5000);
measure_exec_time_rand('maximum_subarray', 10000);
measure_exec_time_rand('maximum_subarray', 15000);
/**
 * Searching maximum subarray inside given array
 * 
 * @param array $a Array for sorting
 * @param integer $low left index, 0 by default
 * @param inreger $high right index, count($a) by default
 * @return array $new_arr left index, right index, maximal sum of element
 */
function maximum_subarray(array $a, $low = 0, $high = NULL)
{
    if (count($a) == 0) {
        throw new Exception("Array is empty", 1);
Example #2
0
 public function after($key, $value, $keyAfter = null)
 {
     if (is_null($keyAfter)) {
         return $this->put($key, $value);
     }
     $keySegments = explode('.', $keyAfter);
     $lastKey = array_pop($keySegments);
     $nestedKey = implode('.', $keySegments);
     if ($nestedKey && $this->offsetExists($nestedKey)) {
         return $this->offsetGet($nestedKey)->items()->after($key, $value, $lastKey);
     }
     $key = str_replace('.', '_', $key);
     $value = $this->validateItem($value);
     $this->items = array_after($this->items, $key, $value, $keyAfter);
     return $this;
 }