$arr=array(19,7,5,1,3,10); $tmp = mergeSort($arr); print_r($tmp); function mergeSort($arr){ $start = 0; $end = count($arr)-1; if($end == 0){ return $arr; } $mid = floor(($start+$end)/2); $arrChunk = array_chunk($arr,$mid+1); $left = mergeSort($arrChunk[0]); $right = mergeSort($arrChunk[1]); $arr3 = sort_data($left,$right); return $arr3; } function sort_data($arr1,$arr2){ $arr=array(); $i=$j=0; $len1=count($arr1); $len2 = count($arr2); while($i < $len1 && $j < $len2){ if($arr1[$i] < $arr2[$j]){ array_push($arr,$arr1[$i]); $i++; } else{ array_push($arr,$arr2[$j]); $j++; } } while($i < count($arr1)){ array_push($arr,$arr1[$i]); $i++; } while($j < count($arr2)){ array_push($arr,$arr2[$j]); $j++; } return $arr; }