C language Bubble Sorting

Source: Internet
Author: User

C language Bubble Sorting

Assume that the sequence Containing n numbers needs to be sorted in ascending order, and the steps of the Bubble Sorting Algorithm are as follows:

1. Compare the adjacent two numbers from the first element in the array storing the sequence to the last element. If the former is large and the latter is small, the two numbers are exchanged;

2. After the fifth stop, the maximum number is stored in the last element of the array. Then, the number of adjacent two numbers is compared from the first element to the last element, if the former is large and the latter is small, the two numbers are exchanged;

3. Repeat Step 1 n-1, and each trip is less than the previous one to complete the request.

Example 1: generate a random number of 10 values less than 100 and use the Bubble Method to sort and output the data in ascending order.

 

# Include

 
  
# Include

  
   
# Define n 10 int main (void) {int a [n], I, j, t; printf ("randomly generate 10 numbers less than 100: \ n "); for (I = 0; I

   
    
A [I + 1]) {t = a [I]; a [I] = a [I + 1]; a [I + 1] = t ;}}} for (I = 0; I

    

     

 

Running result:

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.