Introduction to C Language Program Design Bubble Sorting

Source: Internet
Author: User
Basic Idea of Bubble Sorting
Regard N records as vertically arranged. When each sort is performed from bottom to top, the adjacent records are compared. If the order does not meet the requirements (in reverse order), the records are 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, minimum, minimum, and third small 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
AlgorithmFunction: 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 {flag = 0;
for (j = N; j> I; j --) /* sequential control of inner loops */
If (R [J] {< br> flag = 1;
temp = R [J];
r [J] = R [J-1];
r [J-1] = temp;
}< br> I ++;
}< BR >}/ * 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.