1 ';24 //print_r(quickSort($digits));25 26 function insertSort($digits,$inkey){27 $digits=quickSort($digits);28 $count=count($digits)-1;29 for($j=$count;$j>=0;$j--){30 $key=$digits[$j];31 if($inkey<=$digits[$j]&&$inkey>=$digits[$j-1]){32 $digits[$j]=$inkey;33 break;34 }35 $digits[$j+1]=$key;36 }37 return $digits;38 }39 $inkey=24;40 //print_r(insertSort($digits,$inkey));41 42 function mergeSort($digits){43 if(count($digits)<=1){44 return $digits;45 }46 $mid=count($digits)/2-1;47 $count=count($digits)-1;48 $right=array();49 $left=array();50 for($i=0;$i<=$count;$i++){51 if($i<=$mid){52 $left[]=$digits[$i];53 }else{54 $right[]=$digits[$i];55 }56 }57 $left=mergeSort($left);58 $right=mergeSort($right);59 $result=merge($left,$right);60 return $result;61 }62 function merge($left,$right){63 $llen=count($left);64 $rlen=count($right);65 $count=$llen+$rlen-1;66 $i=0;$j=0;67 while(count($left)>0||count($right)>0){68 if(count($left)>0&&count($right)>0){69 if($left[$i]<=$right[$j]){70 $result[]=$left[$i];71 array_shift($left);72 }else{73 $result[]=$right[$j];74 array_shift($right);75 }76 }else if(count($right)==0){77 $result[]=$left[$i];78 array_shift($left);79 }else if(count($left)==0){80 $result[]=$right[$j];81 array_shift($right);82 }83 }84 return $result;85 }86 print_r(mergeSort($digits));