Algorithm: sort: bubble sort

Source: Internet
Author: User

This should be the most basic sorting algorithm.

Specific operations are as follows:

As you can see, this algorithm is characterized by constantly increasing the number of sorted items like bubbles.

The specific procedure is as follows:

/* Implement with a double loop. Set the External Loop Variable to I and the inner loop variable to j. The External Loop repeats 9 times, and the inner loop repeats 9, 8,..., 1 time in sequence. The two elements for each comparison are related to the inner loop j, which can be identified by a [j] And a [j + 1] respectively, the values of I are 1, 2 ,..., 9. For each I, j values are 1, 2 ,... 10-i. * // * If there are n numbers, n-1 comparison (and exchange) is required ). In the first round, we need to perform the n-I comparison, and in the second I, we need to perform the n-I comparison. */# Include <stdio. h> int main () {int a [10]; int I, j, temp; printf ("please enter ten numbers: \ n"); for (I = 0; I <10; I ++) {scanf ("% d", & a [I]) ;}for (I = 0; I <9; I ++) // perform 9 comparisons in total {for (j = 0; j <9-i; j ++) // perform (10-i) in each query) {if (a [j]> a [j + 1]) // if the first number is greater than the next one, exchange two numbers {temp = a [j]; a [j] = a [j + 1]; a [j + 1] = temp ;}}} for (I = 0; I <10; I ++) {printf ("% d \ t", a [I]);} printf ("\ n "); return 0 ;}

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.