search
HomeDaily ProgrammingPHP KnowledgeDetailed analysis of bubble sort

Bubble sort

For friends who are new to programming and algorithms, the sorting algorithm may not be clear. Next, we will Let’s analyze bubble sorting.

What is the principle of bubble sort?

Traverse an array, during this process, compare the values ​​​​of two adjacent cells: if the former is larger than the latter, the two values ​​​​are swapped. At the end of this process, the maximum value in the array must be placed at the last position.

If the above process is carried out again, it can be determined that the maximum value in the remaining data is placed at the second to last position.

Then continue the above process again, and you can continue to determine that the maximum value in the remaining data is placed at the third to last position.

And so on. . . . .

Illustration:

##The fourth trip1891215 2223The fifth trip91215 182223## Code demonstration:
Original array
18 22 12
15 23 9
First trip 18
22 12 15 9 23
Second trip 18 22 12 9 15 23
The third trip 18 22 9 12 15 23

$arr1 = array(18,22,12,15,23,9);
    $n = count($arr1);
    //控制要进行的躺数
    for($i = 0;$i < $n-1;$i++){
        //在每一趟里面,控制进行比较的次数
        for($k = 0;$k < $n-1-$i;$k++){
            if ($arr1[$k] > $arr1[$k+1]) {
                $temp = $arr1[$k];
                $arr1[$k] = $arr1[$k+1];
                $arr1[$k+1] = $temp;
            }
        }
    }

Finally let’s do To sum up the rules:


1. The number of times to compare pairs from beginning to end and exchange positions is $n-1 times, $n is the total number (array length)

2. Compare the size of two adjacent data every time, and exchange their positions if necessary!

3. The number of comparisons for each trip is one less than the previous trip. The number of comparisons for the first trip is: $n-1

The above is the detailed content of Detailed analysis of bubble sort. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!