Bubble Sorting for [C ++ Interview Questions]

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.
(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 bubbles, 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 <R [J]. key, then the contents of R [J + 1] and R [J] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, 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 ......
Finally, the sequential area R [1. N] can be obtained through n-1 scanning.
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
Keyword sequence: 49 38 65 97 76 13 2749For more information, see animation demonstration]


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

CodeAs follows:

Void bubblesort (seqlist R) {// R (L .. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R: int I, j; Boolean exchange; // exchange flag for (I = 1; I <n; I ++) {// do a maximum of N-1 sort exchange = false; // The exchange flag should be false for (j = n-1; j> = I; j --) // for the current unordered zone R [I .. n] scanning from bottom up if (R [J + 1]. key <R [J]. key) {// exchange record R [0] = R [J + 1]; // R [0] is not a sentinel, only the temporary storage unit R [J + 1] = R [J]; R [J] = R [0]; exchange = true; // exchange occurred, therefore, the switch flag is set to true} If (! Exchange) // This sort order has not been exchanged, and the return algorithm is terminated in advance;} // endfor (External Loop)} // bubblesort

 

4. Algorithm Analysis
(1) The best time complexity of the algorithm
If the initial state of the file is in positive order, a scan can complete the sorting. The required keyword comparison times C and record moving times M both reach the minimum value:
Cmin = n-1
Mmin = 0.
The best time complexity of Bubble Sorting is O (n ).
(2) Worst time complexity of Algorithms
If the initial file is in reverse order, n-1 sort is required. For each sort, we need to compare the n-I keywords (1 ≤ I ≤ N-1), and each comparison must move the record three times to reach the position of the exchange record. In this case, the maximum number of comparisons and moves is reached:
Cmax = N (n-1)/2 = O (n2)
MMax = 3n (n-1)/2 = O (n2)
The worst time complexity of Bubble Sorting is O (n2 ).
(3) The average time complexity of the algorithm is O (n2)
Although the Bubble Sorting does not have to be n-1, the average time performance is much worse than the direct insertion sorting because of the large number of records moving.
(4) algorithm Stability
The Bubble Sorting is in-place and stable.

 

Complete code:

#include 
  
    using namespace STD; void bubblesort (int * List, int Len) {int I, j, temp; for (I = 0; I 
   
     list [J + 1]) {temp = list [J]; list [J] = list [J + 1]; list [J + 1] = temp ;}}int main () {int list [10]; int n = 10, m = 0; cout <"input ten Number: "; for (INT I = 0; I <10; I ++) CIN> list [I]; cout 
    
   
  
Related Article

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.