国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

八大排序算法的 PHP 實現 和 效率測試

jifei / 1243人閱讀

摘要:閑來無事,對基礎的排序算法做了溫故,直接上代碼。同時將代碼貼在了上八大排序算法的實現和效率測試插入排序希爾排序標準冒泡排序快速排序直接選擇排序堆排序歸并排序基數排序

閑來無事,對基礎的排序算法做了溫故,直接上代碼。

同時將代碼貼在了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;
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/21229.html

相關文章

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<