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