Home >Backend Development >PHP Problem >How to implement sorting algorithm in php

How to implement sorting algorithm in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-06 10:33:065532browse

php method to implement sorting algorithm: 1. Bubble sorting, comparing two by two, there is no need to compare the last element in each cycle; 2. Selection sorting, select one as the basic value, and the remaining Compare it with this one, and then change the position.

How to implement sorting algorithm in php

php method to implement sorting algorithm:

1. Bubble sorting:

Comparing pairs, there is no need to compare the last element in each cycle, because the last element is already the largest or smallest.

function maopaoSort ($list)
{
    $len = count($list);
    for ($i = 0; $i < $len - 1; $i++) {
        for ($j = 0; $j < $len - $i - 1; $j++) {
            if ($list[$j] > $list[$j + 1]) {
                $tmp = $list[$j];
                $list[$j] = $list[$j + 1];
                $list[$j + 1] = $tmp;
            }
        }
    }
    return $list;
}

2. Selection sorting:

Select one as the basic value, compare the rest with this one, and then switch positions.

function xuanzeSort ($list)
{
    $len = count($list);
    for ($i = 0; $i < $len - 1; $i++) {
        $pos = $i;
        for ($j = $i + 1; $j < $len; $j++) {
            if ($list[$pos] > $list[$j]) {
                $pos = $j;
            }
        }
        if ($pos != $i) {
            $tmp = $list[$pos];
            $list[$pos] = $list[$i];
            $list[$i] = $tmp;
        }
    }
    return $list;
}

3. Quick sort:

The principle is to take out a ruler value, then divide it into two arrays on the left and right, and compare them respectively

function kuaisuSort ($list)
{
    $len = count($list);
    if ($len <= 1) {//递归出口
        return $list;
    }
    $base = $list[0];//选择一个比较值
    $leftList = $rightList = [];
    for ($i = 1; $i < $len; $i++) {
        if ($base > $list[$i]) {
            $leftList[] = $list[$i];
        } else {
            $rightList[] = $list[$i];
        }
    }
    //递归分别再处理左右两边的数组
    $leftList = kuaisuSort($leftList);
    $rightList = kuaisuSort($rightList);
    return array_merge($leftList, [$base], $rightList);
}

4. Insertion sort:

Assuming that the previous numbers are all in order, you need to insert the nth number into the order

function charuSort ($list)
{
    $len = count($list);
    for ($i = 1; $i < $len; $i++) {
        $tmp = $list[$i];//获取对比元素
        for ($j = $i - 1; $j > 0; $j--) {
            if ($list[$j] > $tmp) {
                $list[$j + 1] = $list[$j];
                $list[$j] = $tmp;
            } else {
                break;
            }
        }
    }
    return $list;
}

Related learning Recommended: php programming (video)

The above is the detailed content of How to implement sorting algorithm in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn