fork(1) download
  1. <?php
  2.  
  3. class BinarySearchAlgorithm {
  4. private $array;
  5. private $value;
  6. public $firstInd;
  7. private $lastInd;
  8.  
  9.  
  10. public function Search($array, $value){
  11. $arrLength = sizeof($array);
  12.  
  13. if (
  14. // Проверка на пустой массив или позицию за пределами массива
  15. !$arrLength ||
  16. $value < $arrLength[0] ||
  17. $value > $array[$arrLength - 1]
  18. ) {
  19. return false;
  20. }
  21.  
  22. $firstInd = 0;
  23. $lastInd = $arrLength - 1;
  24.  
  25. if($array[$lastInd] <= $value){
  26. return $lastInd;
  27. }
  28. if($array[$firstInd] >= $value){
  29. return $firstInd;
  30. }
  31.  
  32. while($firstInd <= $lastInd){
  33. $middleInd = (int)floor($firstInd + $lastInd) / 2;
  34.  
  35. if($value > $array[$middleInd]){
  36. $firstInd = $middleInd + 1;
  37. }
  38.  
  39. elseif($value < $array[$middleInd]){
  40. $lastInd = $middleInd - 1;
  41. }
  42.  
  43. else {
  44. return $middleInd;
  45. }
  46. }
  47. return (int)floor($firstInd);
  48. }
  49. }
  50.  
  51. $array = [1, 2, 2, 2, 2, 2, 8, 8];
  52.  
  53. $obj = new BinarySearchAlgorithm;
  54.  
  55. //var_dump($obj->Search($array, 4));
  56.  
  57.  
  58.  
  59. class SubArrlgorithm{
  60. private $array;
  61. private $maxLocal;
  62. public $maximum;
  63. static $maxLong;
  64.  
  65. public function getMaximumSum($array){
  66.  
  67. $maxLocal = 0;
  68. $maximum = 0;
  69. $maxLong = PHP_INT_MAX;
  70.  
  71. foreach ($array as $item){
  72. if($maxLong - $maxLocal < $item){
  73. print_r("Sum is too large");
  74. }
  75.  
  76. $maxLocal += $item;
  77.  
  78. if($maxLocal < 0){
  79. $maxLocal = 0;
  80. }
  81.  
  82. if($maxLocal > $maximum){
  83. $maximum = $maxLocal;
  84. }
  85. }
  86. return $maximum;
  87. }
  88. }
  89.  
  90. $array1 = [1, 2, 4, -6, 7, 8, 9];
  91. $sumAlg = new SubArrlgorithm;
  92. print_r($sumAlg->getMaximumSum($array1));
  93.  
  94.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
25