mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 23:28:14 +01:00
45 lines
713 B
PHP
45 lines
713 B
PHP
<?php
|
|
|
|
function merge(array $arr1,array $arr2){
|
|
$result=[];
|
|
$i=0;
|
|
$j=0;
|
|
while ($i<count($arr1)&&$j<count($arr2)){
|
|
if ($arr2[$j]>$arr1[$i]){
|
|
$result[]=$arr1[$i];
|
|
$i++;
|
|
}else{
|
|
$result[]=$arr2[$j];
|
|
$j++;
|
|
}
|
|
}
|
|
while ($i<count($arr1)){
|
|
$result[]=$arr1[$i];
|
|
$i++;
|
|
}
|
|
while ($j<count($arr2)){
|
|
$result[]=$arr2[$j];
|
|
$j++;
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mergeSort(array $arr){
|
|
if (count($arr)<=1)return $arr;
|
|
$mid=floor(count($arr)/2);
|
|
$left=mergeSort(array_slice($arr,0,$mid));
|
|
$right=mergeSort(array_slice($arr,$mid));
|
|
|
|
return merge($left,$right);
|
|
|
|
|
|
}
|
|
|
|
|