<?php

/**
 * 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)
{
 public function before($key, $value, $keyBefore = null)
 {
     if (is_null($keyBefore)) {
         return $this->prepend($key, $value);
     }
     $keySegments = explode('.', $keyBefore);
     $lastKey = array_pop($keySegments);
     $nestedKey = implode('.', $keySegments);
     if ($nestedKey && $this->offsetExists($nestedKey)) {
         return $this->offsetGet($nestedKey)->items()->before($key, $value, $lastKey);
     }
     $key = str_replace('.', '_', $key);
     $value = $this->validateItem($value);
     $this->items = array_before($this->items, $key, $value, $keyBefore);
     return $this;
 }