一、冒泡排序法
function bubbleSort(array $array) {
$array_size = count($array);
for($i = 0; $i < $array_size; $i ++) {
for($j = 0; $j < $array_size; $j ++) {
if ($array[$i] < $array[$j]) {
$tem = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $tem;
}
}
}
return $array;}
二、选择排序法
function selectionSort(array $array) {
$length = count($array);
for($i = 0; $i < $length; $i ++) {
$min = $i;
for($j = $i + 1; $j < $length; $j ++) {
if ($array[$j] < $array[$min]) {
$min = $j;
}
}
$tmp = $array[$min];
$array[$min] = $array[$i];
$array[$i] = $tmp;
}
return $array;}
三、插入排序法
function insertionSort(array $array) {
$count = count($array);
for($i = 1; $i < $count; $i ++) {
$j = $i - 1;
// second element of the array
$element = $array[$i];
while ( $j >= 0 && $array[$j] > $element ) {
$array[$j + 1] = $array[$j];
$array[$j] = $element;
$j = $j - 1;
}
}
return $array;}
四、Shell排序法
function shellSort(array $array) {
$gaps = array(
1,
2,
3,
4,
6
);
$gap = array_pop($gaps);
$length = count($array);
while ( $gap > 0 ) {
for($i = $gap; $i < $length; $i ++) {
$tmp = $array[$i];
$j = $i;
while ( $j >= $gap && $array[$j - $gap] > $tmp ) {
$array[$j] = $array[$j - $gap];
$j -= $gap;
}
$array[$j] = $tmp;
}
$gap = array_pop($gaps);
}
return $array;}
五、Comb Sort
(http://en.wikipedia.org/wiki/Comb_sort)
function combSort(array $array) {
$gap = count($array);
$swap = true;
while ( $gap > 1 || $swap ) {
if ($gap > 1)
$gap /= 1.25;
$swap = false;
$i = 0;
while ( $i + $gap < count($array) ) {
if ($array[$i] > $array[$i + $gap]) {
// swapping the elements.
list($array[$i], $array[$i + $gap]) = array(
$array[$i + $gap],
$array[$i]
);
$swap = true;
}
$i ++;
}
}
return $array;}
六、归并排序
(http://en.wikipedia.org/wiki/Merge_sort)
function mergeSort(array $array) {
if (count($array) <= 1)
return $array;
$left = mergeSort(array_splice($array, floor(count($array) / 2)));
$right = mergeSort($array);
$result = array();
while ( count($left) > 0 && count($right) > 0 ) {
if ($left[0] <= $right[0]) {
array_push($result, array_shift($left));
} else {
array_push($result, array_shift($right));
}
}
while ( count($left) > 0 )
array_push($result, array_shift($left));
while ( count($right) > 0 )
array_push($result, array_shift($right));
return $result;}
七、快速排序
function quickSort(array $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));}
八、Permutation sort
(http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort)
function permutationSort($items, $perms = array()) {
if (empty($items)) {
if (inOrder($perms)) {
return $perms;
}
} else {
for($i = count($items) - 1; $i >= 0; -- $i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$res = permutationSort($newitems, $newperms);
if ($res) {
return $res;
}
}
}}
function inOrder($array) {
for($i = 0; $i < count($array); $i ++) {
if (isset($array[$i + 1])) {
if ($array[$i] > $array[$i + 1]) {
return False;
}
}
}
return True;}
九、Radix sort
(http://en.wikipedia.org/wiki/Radix_sort)
// Radix Sort for 0 to 256function radixSort($array) {
$n = count($array);
$partition = array();
for($slot = 0; $slot < 256; ++ $slot) {
$partition[] = array();
}
for($i = 0; $i < $n; ++ $i) {
$partition[$array[$i]->age & 0xFF][] = &$array[$i];
}
$i = 0;
for($slot = 0; $slot < 256; ++ $slot) {
for($j = 0, $n = count($partition[$slot]); $j < $n; ++ $j) {
$array[$i ++] = &$partition[$slot][$j];
}
}
return $array;}