Skip to content

Commit

Permalink
Merge pull request #120 from BuddhikaShaminda/master
Browse files Browse the repository at this point in the history
add merge sort using php
  • Loading branch information
felipecustodio authored Oct 29, 2018
2 parents 03d86b3 + ba50cde commit 53a6dda
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions data_structures/merge sort/merge_sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
function merge_sort($my_array){
if(count($my_array) == 1 ) return $my_array;
$mid = count($my_array) / 2;
$left = array_slice($my_array, 0, $mid);
$right = array_slice($my_array, $mid);
$left = merge_sort($left);
$right = merge_sort($right);
return merge($left, $right);
}
function merge($left, $right){
$res = array();
while (count($left) > 0 && count($right) > 0){
if($left[0] > $right[0]){
$res[] = $right[0];
$right = array_slice($right , 1);
}else{
$res[] = $left[0];
$left = array_slice($left, 1);
}
}
while (count($left) > 0){
$res[] = $left[0];
$left = array_slice($left, 1);
}
while (count($right) > 0){
$res[] = $right[0];
$right = array_slice($right, 1);
}
return $res;
}
$test_array = array(100, 54, 7, 2, 5, 4, 1);
echo "Original Array : ";
echo implode(', ',$test_array );
echo "\nSorted Array :";
echo implode(', ',merge_sort($test_array))."\n";
?>

0 comments on commit 53a6dda

Please sign in to comment.