Thoughts and principles of Bubble Sorting

Source: Internet
Author: User

1. Sorting Method

The sorted record array R [1. N] is vertically arranged. Each record R [I] is considered as a bubble with the weight of R [I]. Key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.

The Bubble Sorting is in-place and stable.

(1) initial
r [1 .. n] is an unordered area.
(2) First scan
the weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe areas, the positions of the two bubbles are exchanged. That is, compare (R [N], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [J + 1], R [J]), if R [J + 1]. key
when the first scan is completed, the "lightest" bubble floated to the top of the range, that is, the record with the smallest keyword is placed on the highest position R [1.
(3) second scan
scan R [2 .. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
last, n-1 area R [1 .. n]

Note:
During the I-trip scan, R [1 .. I-1] and R [I.. N] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R [I]. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example
The process of Bubble Sorting 【

]

3. SortingAlgorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to false before each sort starts. If an exchange occurs during the sorting process, set it to true. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms

void bubblesort (int * r, int N)
{ // R [0 .. n-1] is the file to be sorted. It is scanned from bottom to top, sort R by bubble

int I, J;
Boolean exchange; // exchange flag

For (I = 0; I <n-1; I ++)// Sorting a maximum of N-1 troughs
{

Exchange = false;// The exchange flag should be false before the sorting starts.

For (j = n-1; j> I; j --)// Scan the current unordered zone R [I .. n-1] from bottom up
{

If (R [J] <R [J-1])// Exchange records

{

Int TMP = R [J-1];// Temporary storage unit

R [J-1] = R [J];

R [J] = TMP;

exchange = true; // exchange occurred, therefore, set the switching flag to true
}

}

If (! Exchange)// This sort order is not exchanged and the algorithm is terminated in advance
Return;

}// Endfor (External Loop)
}// Bubblesort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.