Algorithm analysis and improvement of bubble sorting (reproduced)

xiaoxiao2021-03-06  57

Algorithm analysis and improvement of bubbling sorting

The basic idea of ​​switching sort is: Two two two-two keywords to be sorted, and found that the two recorded sequences are exchanged, until there is no reagent record.

The main sorting method of application exchange sorting basic ideas is: bubble sorting and rapid sorting.

Bubbling 1, sorting method

The recording array R [1..n] of the sorted record is vertically arranged, each record R [i] is considered to be a bubble of R [I] .Key. According to the principle of light bubbles, it cannot be scanned from the bottom, from the bottom to the array R: Anyone who scans the violation of this principles will make it "floating". So repeated, until the last two bubbles are in the light, the heavy people are below.

(1) Initial

R [1..n] is a disorder area.

(2) First scan

The weight of the two bubbles is compared from the bottom of the unordered area, and if the light is discovered, the weight is on the top, then exchange the position. That is, compared (R [N], R [N-1]), (R [N-1], R [N-2]), ..., (R [2], R [1]); for each pair Bubble (R [J 1], R [J]), if R [J 1] .Key

When the first scan is completed, the "lightest" bubble floats to the top of the interval, that is, the smallest record of the keyword is placed on the highest position R [1].

(3) Second scan

Scan R [2..n]. When the scan is completed, the bubbles of "Light" floats to R [2].

Finally, after the N-1 scan can be obtained, the orderless area R [1..n]

note:

When the scan is scanned, R [1..i-1] and R [i..n] are current ordered regions and disorderless regions, respectively. The scan is still upward from the bottom of the disorder zone until the top of the area. When the scan is completed, the lightest bubble in this area floats to the top position R [I], and the result is that R [1..i] becomes a new ordered area.

2. Example of bubble sorting process

The keyword sequence is 49 38 65 97 76 13 27

49 files for bubble ordering process [

See animation demonstrations】

3, sort algorithm

(1) Analysis

Because each sort has increased the ordered zone, there is a bubble after being sorted by N-1, there is N-1 bubble in the ordered zone, and the weight of the bubble in the disorder area is always greater than ordered. The weight of bubbles in the district, so the entire bubble sorting process needs to be sorted by N-1.

If there is no exchange of bubble position in a certain sort, all air bubbles in the disorderly zone to be sorted are in the principle of being on the principle, and therefore, the bubble sorting process can be sorted termination. To this end, in the algorithm given below, a Boolean Exchange is introduced, and it will be set to false before the start of each trip. If you have exchanged during the sorting process, it is set to true. Check Exchange at the end of each, if there is no exchange, the algorithm is terminated, no longer sorted.

(2) Specific algorithm

Void Bubblesort (SEQLIST R)

{//R (L..n) is the file to be sorted, using the downward scan, to be sorted by R

INT I, J;

Boolean Exchange; // Exchange Sign

For (i = 1; i

Exchange = false; // Before the start of this, the exchange flag should be false

For (j = n-1; j> = i; j--) // Scan IF on the current sequencing area R [i..n] (R [J 1] .Key

R [0] = r [j 1]; // r [0] is not a sentinel, only a temporary unit

R [J 1] = R [J];

R [j] = r [0];

Exchange = true; // has an exchange, so the exchange flag is true.

}

If (! Exchange) // This slap has not exchanged, the early termination algorithm

Return;

} // Endfor (external loop)

} // bubblesort

4, algorithm analysis

(1) The best time complexity of the algorithm

If the initial state of the file is positive or sequence, you can complete the sort. The number of keywords required for the desired keyword C and the number of records M movement are minimum:

C

min = n-1

M

MIN = 0.

The best time complexity of bubble sorting is O (n).

(2) The worst time complexity of the algorithm

If the initial file is in the reverse order, N-1 sorting is required. Every time you sort the comparison of the N-I keyword (1 ≤ i ≤ N-1), and each comparison must move the record three times to reach the exchange record position. In this case, the number of comparisons and movements reach the maximum:

C

MAX = N (N-1) / 2 = O (n

2)

M

MAX = 3N (N-1) / 2 = o (N

2)

The worst time complexity of bubble sorting is O (N

2).

(3) The average time complexity of the algorithm is O (N2)

Although the bubbling sort is not necessarily N-1, since its record movement is much more, the average time performance is much different than the direct insertion.

(4) Algorithm stability

Bubble Sort is sorted in place and it is stable.

5, algorithm improvement

The above-mentioned bubble sorting can also be made as follows:

(1) Remember the last exchange of occurrence position Lastexchange bubble sort

In each scan, remember the location of the last exchange that occurs at the last exchange, (adjacent the adjacent record before this location). At the beginning of the next sort, R [1..lastexchange-1] is an ordered zone, R [Lastexchange..n] is an unordered area. In this way, a sort may make the current ordered zone to expand multiple records, thereby reducing the number of sorting. Specific algorithm [see exercises].

(2) Change the bubble sorting of the scanning direction

1 bubble sorting asymmetry

Can be scanned to complete the sort:

Only the lightest air bubble is located in the R [N], the remaining air bubbles have been ranked, and then only one scan can be sorted.

[Example] The initial keyword sequence 12, 18, 42, 44, 45, 67, 94, 10 need only one scan.

Need N-1 scan to complete the order:

When only the most heavy bubbles are located in the R [1], the remaining air bubbles are ranked, and the N-1 scan can still be sorted.

[Example] Seven scans required for the initial keyword sequence: 94, 10, 12, 18, 42, 44, 45, 67.

2 reasons for dissemination

Each scan can only make the maximum bubble "sink", so the N-1 scan is required when the maximum bubble located at the top is sinking to the bottom.

3 Improved asymmetry

Alternately change the scanning direction during the sorting process, it can improve asymmetry. Specific algorithm [see exercises].

转载请注明原文地址:https://www.9cbs.com/read-109219.html

New Post(0)