资讯专栏INFORMATION COLUMN

八大排序算法的 PHP 实现 和 效率测试

jifei / 1242人阅读

摘要:闲来无事,对基础的排序算法做了温故,直接上代码。同时将代码贴在了上八大排序算法的实现和效率测试插入排序希尔排序标准冒泡排序快速排序直接选择排序堆排序归并排序基数排序

闲来无事,对基础的排序算法做了温故,直接上代码。

同时将代码贴在了gist上:八大排序算法的 PHP 实现 和 效率测试

= 0 && $lists[$j] > $key; $j--) {
            $lists[$j + 1] = $lists[$j];
            $lists[$j] = $key;
        }
    }
    return $lists;
}
/**
 * 希尔排序 标准
 *
 * @param array $lists
 * @return array
 */
function shell_sort(array $lists)
{
    $n = count($lists);
    $step = 2;
    $gap = intval($n / $step);
    while ($gap > 0) {
        for ($gi = 0; $gi < $gap; $gi++) {
            for ($i = $gi; $i < $n; $i += $gap) {
                $key = $lists[$i];
                for ($j = $i - $gap; $j >= 0 && $lists[$j] > $key; $j -= $gap) {
                    $lists[$j + $gap] = $lists[$j];
                    $lists[$j] = $key;
                }
            }
        }
        $gap = intval($gap / $step);
    }
    return $lists;
}
/**
 * 冒泡排序
 *
 * @param array $lists
 * @return array
 */
function bubble_sort(array $lists)
{
    $num = count($lists);
    for ($i = 0; $i < $num; $i++) {
        for ($j = $i + 1; $j < $num; $j++) {
            if ($lists[$i] > $lists[$j]) {
                $key = $lists[$i];
                $lists[$i] = $lists[$j];
                $lists[$j] = $key;
            }
        }
    }
    return $lists;
}
/**
 * 快速排序
 *
 * @param array $lists
 * @param $left
 * @param $right
 * @return array
 */
function quick_sort(array &$lists, $left = 0, $right = null)
{
    if (is_null($right)) {
        $right = count($lists) - 1;
    }
    if ($left >= $right) {
        return $lists;
    }
    $key = $lists[$left];
    $low = $left;
    $high = $right;
    while ($left < $right) {
        while ($left < $right && $lists[$right] > $key) {
            $right--;
        }
        $lists[$left] = $lists[$right];
        while ($left < $right && $lists[$left] < $key) {
            $left++;
        }
        $lists[$right] = $lists[$left];
    }
    $lists[$right] = $key;
    quick_sort($lists, $low, $left - 1);
    quick_sort($lists, $left + 1, $high);
    return $lists;
}
/**
 * 直接选择排序
 *
 * @param array $lists
 * @return array
 */
function select_sort(array $lists)
{
    $n = count($lists);
    for ($i = 0; $i < $n; $i++) {
        $key = $i;
        for ($j = $i + 1; $j < $n; $j++) {
            if ($lists[$j] < $lists[$key]) {
                $key = $j;
            }
        }
        $val = $lists[$key];
        $lists[$key] = $lists[$i];
        $lists[$i] = $val;
    }
    return $lists;
}
/**
 * 堆排序
 *
 * @param array $lists
 * @return array
 */
function heap_sort(array $lists)
{
    $n = count($lists);
    build_heap($lists);
    while (--$n) {
        $val = $lists[0];
        $lists[0] = $lists[$n];
        $lists[$n] = $val;
        heap_adjust($lists, 0, $n);
        //echo "sort: " . $n . "	" . implode(", ", $lists) . PHP_EOL;
    }
    return $lists;
}
function build_heap(array &$lists)
{
    $n = count($lists) - 1;
    for ($i = floor(($n - 1) / 2); $i >= 0; $i--) {
        heap_adjust($lists, $i, $n + 1);
        //echo "build: " . $i . "	" . implode(", ", $lists) . PHP_EOL;
    }
    //echo "build ok: " . implode(", ", $lists) . PHP_EOL;
}
function heap_adjust(array &$lists, $i, $num)
{
    if ($i > $num / 2) {
        return;
    }
    $key = $i;
    $leftChild = $i * 2 + 1;
    $rightChild = $i * 2 + 2;
    if ($leftChild < $num && $lists[$leftChild] > $lists[$key]) {
        $key = $leftChild;
    }
    if ($rightChild < $num && $lists[$rightChild] > $lists[$key]) {
        $key = $rightChild;
    }
    if ($key != $i) {
        $val = $lists[$i];
        $lists[$i] = $lists[$key];
        $lists[$key] = $val;
        heap_adjust($lists, $key, $num);
    }
}
/**
 * 归并排序
 *
 * @param array $lists
 * @return array
 */
function merge_sort(array $lists)
{
    $n = count($lists);
    if ($n <= 1) {
        return $lists;
    }
    $left = merge_sort(array_slice($lists, 0, floor($n / 2)));
    $right = merge_sort(array_slice($lists, floor($n / 2)));
    $lists = merge($left, $right);
    return $lists;
}
function merge(array $left, array $right)
{
    $lists = [];
    $i = $j = 0;
    while ($i < count($left) && $j < count($right)) {
        if ($left[$i] < $right[$j]) {
            $lists[] = $left[$i];
            $i++;
        } else {
            $lists[] = $right[$j];
            $j++;
        }
    }
    $lists = array_merge($lists, array_slice($left, $i));
    $lists = array_merge($lists, array_slice($right, $j));
    return $lists;
}
/**
 * 基数排序
 *
 * @param array $lists
 * @return array
 */
function radix_sort(array $lists)
{
    $radix = 10;
    $max = max($lists);
    $k = ceil(log($max, $radix));
    if ($max == pow($radix, $k)) {
        $k++;
    }
    for ($i = 1; $i <= $k; $i++) {
        $newLists = array_fill(0, $radix, []);
        for ($j = 0; $j < count($lists); $j++) {
            $key = $lists[$j] / pow($radix, $i - 1) % $radix;
            $newLists[$key][] = $lists[$j];
        }
        $lists = [];
        for ($j = 0; $j < $radix; $j++) {
            $lists = array_merge($lists, $newLists[$j]);
        }
    }
    return $lists;
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/21229.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<