C language Bubble Sorting

Source: Internet
Author: User

  Basic Idea of Bubble Sorting

  Regard n records as vertically arranged, and compare each pair of adjacent records from bottom to top in each sort. If the order does not meet the demand (Reverse Order), it is exchanged. At the end of each sort, the record with the smallest keyword in the sorting range can be raised to the corresponding position on the table like a bubble. The entire sorting process involves a total of N-1 queries, sort the smallest, secondary, and third smallest keywords in sequence... The first, the second, and the third records of the "to" table... Location.

Initial model: 1st trips, 2nd trips, 3rd trips, 4th trips, 5th trips, 6th trips

38 12 12 12 12 12 12 12

20 38 20 20 20 20 20

46 20 38 25 25 25 25

38 46 25 38 38 38 38 38

74 38 46 38 38 38 38 38 38

91 74 38 46 46 46 46

12 91 74 74 74 74

25 25 91 91 91 91 91 91

/*

Title: Bubble Sorting

Author: Li Aimin

Date: May 1, 2007

Algorithm function: the Bubble sorting algorithm is used to sort all elements in a linear table r with a length of n in ascending order by keywords.

*/

# Include <stdio. h>

Void bubblesort (int r [], int n)

{/* Elements are stored in r [1] to r [n] */

Int I, j, flag;

Int temp;

Flag = 1;

I = 1;

While (I <n) & (flag = 1)/* total number of workers in the External Loop Control sorting */

{Flag = 0;

For (j = n; j> I; j --)/* the inner loop controls the sorting process */

If (r [j] <r [J-1])/* compare adjacent elements, if the reverse order is exchanged */

{

Flag = 1;

Temp = r [j];

R [j] = r [J-1];

R [J-1] = temp;

}

I ++;

}

}/* Bubblesort */

Void show (int r [], int n)

{

/* Elements are stored in r [1] to r [n] */

Int I;

For (I = 1; I <= n; I ++)

Printf ("% d", r [I]);

Printf ("n ");

  

}

Void main ()

{

Int a [9201], I;

For (I = 0; I <9201; I ++)

A [I] = 9201-i;

// Show (a, 100000 );

Bubblesort (a, 9200 );

Show (a, 9200 );

}

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.