Sort algorithms for algorithm learning: Bubble Sorting

Source: Internet
Author: User

Bubble Sorting:Unlike insertion sorting, Bubble Sorting is mainly done through "Exchange.


Basic Idea:

1. Compare the keywords of the first record with those of the second record. If it is in reverse order (record [1]. key> record [2]. key), the two records are exchanged, and then the keywords of the second record and the third record are compared.

2. Likewise, until the keyword of the n-1 record and the n record is placed at the position of the last record. Complete the first Bubble sorting. As a result, the record with the largest keyword is placed at the position of the last record.

3. Perform the second Bubble sorting and perform the same operation on the first n-1 records. The result is that the record with the keyword size is placed on the bit of the n-1 record. The I-byte Bubble Sorting compares the keywords of two adjacent records from record [1] to record [n-I + 1] sequentially, and exchanges adjacent records in the reverse order, the result is that the record with the largest keyword in the n-I + 1 record is exchanged to the position at n-I + 1.

4. K (1 <= k <n) Bubble Sorting is required throughout the sorting process, the condition for determining the end Of the Bubble Sorting is "the exchange record operation has not been performed during the sorting process ".


The following uses eight records as an example for analysis:

Initial Keyword:49 38 65 97 76 13 27 49

First bubble: 49 38 65 97 76 13 27 49 // compare the keywords of the first record with the second record, reverse order, exchange

38 49 65 97 76 13 27 49


38 49 65 97 76 13 27 49 // compare the keywords of the second and third records, reverse order, exchange

38 65 49 97 76 13 27 49


38 65 49 97 76 13 27 49 // compare the keywords of the third and fourth records, in the forward order, unchanged

38 65 49 97 76 13 27 49


38 65 49 97 76 13 27 49 // compare the keywords of the fourth and fifth records, reverse order, and exchange

38 65 49 76 97 13 27 49


38 65 49 76 97 13 27 49 // compare the keywords of the fifth and sixth records, reverse order, and exchange

38 65 49 76 13 97 27 49


38 65 49 76 13 97 27 49 // compare the keywords of the sixth and seventh records, reverse order, and exchange

38 65 49 76 13 27 97 49


38 65 49 76 13 27 97 49 // compare the keywords of the seventh and eighth records, reverse order, exchange

38 65 49 76 13 27 49 97

The first Bubble Sorting ends, and the maximum value of 97 is placed on the last record. Sort bubbles in sequence:

38 65 49 76 13 27 49 97

The second Bubble Sorting ends: 38 65 49 13 27 49 76 97

End of the third Bubble Sorting: 38 49 13 27 49 65 76 97

End of the fourth Bubble Sorting: 38 13 27 49 65 76 97

The fifth Bubble Sorting ended: 38 13 27 49 65 76 97

End of the sixth Bubble Sorting: 13 27 38 49 65 76 97

The seventh hop is Bubble sorting, and no records are exchanged. Therefore, the Bubble Sorting ends, and the final Bubble sorting order is:

13 27 38 49 49 65 76 97


Sample Code (implemented in C ):
/*************************************** * ***************************** Author: li Bing Date: 2014-9-6 Email: [email protected] @ array: the pointer to the records @ num: the length of the records *********************************** * ********************************/void bubblesort (INT Array [], int num) {If (array = NULL | num <0) return; int I, j, TMP; for (I = 0; I <num; I ++) for (j = 1; j <num-I; j ++) if (array [J-1]> array [J]) {TMP = array [J-1]; array [J-1] = array [J]; array [J] = TMP ;}}


The above Code strictly follows the bubble sequence, but does not determine whether the bubble sequence ends. Next, we will optimize it and set a flag. If this trip is switched, it will be true; otherwise, it will be false. Obviously, if there is no exchange, the sorting is completed.

Sample Code 2 (in C ):

/*************************************** * ***************************** Author: li Bing Date: 2014-9-6 Email: [email protected] @ array: the pointer to the records @ num: the length of the records *********************************** * ********************************/void bubblesort (INT Array [], int num) {Int J, TMP; bool flag; flag = true; // the flag for judging the end of Bubble sorting. While (FLAG) {flag = false; For (j = 1; j <num; j ++) if (array [J-1]> array [J]) {TMP = array [J-1]; array [J-1] = array [J]; array [J] = TMP; flag = true;} num --;}}

Summary:

1. The total time complexity of Bubble Sorting is O (n ^ 2 ).

2. You can set the flag of the Bubble Sorting completion judgment flag. If a Bubble Sorting does not exchange records, the Bubble Sorting is complete.

3. Pay attention to the boundary judgment in the sample code.


References:

1. Edited by Yan Weimin Wu Weidong, data structure (C language version)

2. http://blog.csdn.net/morewindows/article/details/6668714

3. http://blog.csdn.net/to_be_it_1/article/details/37866391



















Sort algorithms for algorithm learning: Bubble Sorting

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.