search
HomeBackend DevelopmentPHP ProblemHow to implement sorting algorithm in php

How to implement sorting algorithm in php

Oct 06, 2020 am 10:33 AM
phpSorting Algorithm

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools