search
Homephp教程PHP开发Exchange sort—Bubble Sort (Bubble Sort)

Exchange sort—Bubble Sort (Bubble Sort)

Dec 19, 2016 pm 02:04 PM
Bubble Sort

Exchange sorting mainly compares the key codes of the records to be sorted in pairs. If the key codes of the records to be sorted occur, if they are contrary to the sorting requirements, then exchange them. Let’s first look at the bubbling process of sorting columns: Let 1

i=1; //Set up pairwise comparison starting from the first record

If i≥j, one-trip bubbling ends.

Compare r[i].key and r[i+1].key, if r[i].key ≤ r[i+1].key, do not exchange, turn to ⑤

when r[i].key> ;r[i+1].key, r[0]=r[i]; r[i]=r[i+1]; r[i+1]=r[0]; change r[i] Exchange

i=i+1 with r[i+1]; adjust the pairwise comparison of the next two records and switch to the ②

bubble sorting method: for a table of n records, the first bubble is A record r[n] with the largest key code, the second bubble is performed on the table of n-1 records, and then a record r[n-1] with the largest key code is obtained. Repeat this until n records are pressed by the key code. Ordered table.


【Algorithm 10.6】


j=n; //Start from the table with n records

If j

i=1; //One trip of bubbling, setting starts from the first record Perform pairwise comparison,

If i ≥ j, one bubbling trip ends, j=j-1; the number of records in the bubble table is -1, go to ②

to compare r[i].key and r[i+1 ].key, if r[i].key≤r[i+1].key, no exchange, turn to ⑤

When r[i].key>r[i+1].key, r[i] r[i+1]; Exchange r[i] and r[i+1]

i=i+1; Adjust the next two records to compare them in pairs, go to ④

【Efficiency Analysis】

Space efficiency: Only one auxiliary unit is used.

Time efficiency: A total of n-1 bubbling operations are required. One bubbling operation for a table with j records requires j-1 key code comparisons.

Number of moves:

Exchange sort—Bubble Sort (Bubble Sort)Best case: the columns to be sorted are already in order, no need to move


Worst case: three moves are required after each comparison,

Exchange sort—Bubble Sort (Bubble Sort)



For more exchange sorting-bubble sorting (Bubble Sort) related articles, please pay attention to 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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!