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"; ?>