快递排序
快递排序PHP实现
$unsorted = array(2,4,5,63,4,5,63,2,4,43); function quicksort($array) { if (count($array) == 0) return array(); $pivot = $array[0]; $left = $right = array(); for ($i = 1; $i < count($array); $i++) { if ($array[$i] < $pivot) $left[] = $array[$i]; else $right[] = $array[$i]; } return array_merge(quicksort($left), array($pivot), quicksort($right)); } $sorted = quicksort($unsorted); print_r($sorted);
var a = [2,4,5,63,4,5,63,2,4,43]; function quicksort(arr){ if (arr.length == 0) return []; var left = new Array(); var right = new Array(); var pivot = arr[0]; for (var i = 1; i < arr.length; i++) { if (arr[i] < pivot) { left.push(arr[i]); } else { right.push(arr[i]); } } return quicksort(left).concat(pivot, quicksort(right));} document.write(quicksort(a));