# C Language # Summary of Bubble sorting,

Source: Internet
Author: User

# C Language # Summary of Bubble sorting,

For beginners, let's summarize the Bubble sorting method during the code learning process.

The basic idea of Bubble Sorting: compare two adjacent numbers each time, and adjust a small number to the front.
If there are 6 numbers: 9, 8, 5, 4, 2, 0
For the first time, the first two numbers are 8 and 9, and the second and third numbers are 9 and 5 )...... In this case, perform a total of five times to obtain the order.

As you can see, the large number has sunk to the bottom, and the decimal number has risen. The smallest number 0 has floated up to a position, and the maximum number 9 has been obtained after a comparison.

Next, compare the remaining five numbers. (, 0) for a new round of comparison, in order to make the second largest bottom. Perform the second comparison using the preceding method. Get the second largest number 8.

According to this rule, we can infer that we must compare the six numbers five times to sort the six numbers in order of size.
The first trip is five times, and the second trip is four times ...... The fifth round is compared once.
Rule: If n numbers exist, n-1 comparisons are performed. In the first comparative Chinese medicine, we compared the numbers of n-j times, and compared the numbers of n-j times.
I write Bubble Sorting as a function

1 # include <stdio. h> 2/* ========================================== ======================================= 3 * Function Name: main () 4 * function: main Function 5 * entry parameter: 6 * exit parameter: 7 * description: use the Bubble Method to sort the given array 8 * = ============================================= */9 void main () 10 {11 void qipao_paixu (int num []); 12 int a [10] = {655,23, 56,2345, 3897,36, 478,31, 90,208}; 13 int I; 14 printf ("original sequence:"); 15 for (I = 0; I <10; I ++) 16 printf ("% 5d", a [I]); 17 printf ("\ n"); 18 printf ("Bubble Sorting \ n"); 19 qipao_paixu (a); 20 printf ("sorted sequence :"); 21 for (I = 0; I <10; I ++) 22 printf ("% 5d", a [I]); 23 24} 25/* ======================================== ======================================= 26 * Function Name: qipao_paixu () 27 * function: Bubble Sorting 28 * entry parameter: 29 * exit parameter: 30 * description: 10 elements in the array are bubble sorted 31 * ============================== ============================= */32 void qipao_paixu (int num []) 33 {34 int I, j, temp; 35 for (j = 0; j <9; j ++) // control j comparison 36 for (I = 0; I <9-j; I ++) // control the n-j count to compare 37 {38 if (num [I]> num [I + 1]) // judge the size of the adjacent numbers in the sequence. 39 {40 temp = num [I]; // place the numbers in the decimal places forward, and the large numbers sink to the bottom. 41 num [I] = num [I + 1]; 42 num [I + 1] = temp; 43} 44 45} 46 47}

Compiled and run by codeblock

Newbie, I also hope to give more corrections and will often summarize and discuss them later.

Bruce Lee

2016.6.6

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.