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: