C language for Bubble Sorting

Source: Internet
Author: User

Data structure... amount, although I had attended this course, I haven't looked at it for a long time, and I forgot all of it ....

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
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 );
}

 

Original post address: http://www.cnblogs.com/emanlee/archive/2008/04/28/1174071.html

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.