大多数基本的方法都已经讲过了,我会试着看看其他类型的排序
用SPL排序
碎片堆
class SimpleHeapSort extends SplHeap {
public function compare($a, $b) {
return strcmp($a, $b);
}
}
// Let's populate our heap here (data of 2009)
$heap = new SimpleHeapSort();
$heap->insert("a");
$heap->insert("b");
$heap->insert("c");
echo implode(PHP_EOL, iterator_to_array($heap));
输出
c
b
a
SplMaxHeap
SplMaxHeap类提供堆的主要功能,将最大值保持在顶部。
$heap = new SplMaxHeap();
$heap->insert(1);
$heap->insert(2);
$heap->insert(3);
SplMinHeap
SplMinHeap类提供了堆的主要功能,将最小值保持在顶部。
$heap = new SplMinHeap ();
$heap->insert(3);
$heap->insert(1);
$heap->insert(2);
其他类型的排序
冒泡排序
摘自维基百科关于冒泡排序的文章:
Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.
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;
}
选择排序
摘自维基百科关于选择排序的文章:
在计算机科学中,选择排序是一种排序算法,特别是就地比较排序。它有O(n2)的时间复杂度,使得它在大型列表上效率低下,并且通常比类似的插入排序执行得更差。选择排序以其简单性而闻名,并且在某些情况下,特别是在辅助内存有限的情况下,它比更复杂的算法具有性能优势。
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;
}
Shellsort
来自维基百科关于Shellsort的文章:
Shellsort,也称为Shell排序或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;
}
梳子排序
摘自维基百科关于梳状排序的文章:
梳状排序是由Wlodzimierz Dobosiewicz在1980年设计的一种相对简单的排序算法。后来,它在1991年被斯蒂芬·莱西和理查德·博克斯重新发现。梳状排序改进了冒泡排序。
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;
}
去排序
摘自维基百科关于归并排序的文章:
在计算机科学中,归并排序(也通常拼写为mergesort)是一种O(n log n)基于比较的排序算法。大多数实现都会产生稳定的排序,这意味着实现会保留排序输出中相等元素的输入顺序
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;
}
快速排序
维基百科上关于快速排序的文章:
快速排序,或分区交换排序,是由Tony Hoare开发的一种排序算法,平均而言,对n个项目进行O(n log n)次比较。在最坏的情况下,它进行O(n2)比较,尽管这种行为很少见。
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));
}
排列排序
来自维基百科关于排列排序的文章:
排列排序,通过生成输入数组/列表的可能排列,直到发现已排序的数组/列表。
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排序的文章:
在计算机科学中,基数排序是一种非比较整数排序算法,它通过将键按具有相同重要位置和值的单个数字分组,对具有整数键的数据进行排序。
// Radix Sort for 0 to 256
function 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;
}