Sort algorithm------Bubble sort bubblesort
Bubble sort is the most basic sort method, it is through the large number of precipitation, not a round of sequencing, the entire sequence of the largest number will be placed in the end, "bubbling is the final appearance of this number in the final visualization of the description", and then because this number has been arranged, so no longer consider, Then the large number of previous numbers is precipitated again, and so on until all the sequences have been lined up.
Specific case Description:
4,8,3,2,1,9,6,7,5
The process is as follows:
(1) The first round of circulation <i=0>:
First number J: 4 and j+1: 8 comparison, 4<8, large number in the rear, not moving, j + +;
4,8,3,2,1,9,6,7,5---------------Results 4,8,3,2,1,9,6,7,5
J J+1
Then J: 8 and j+1: 3 comparison, 8>3, large number in front, exchange two numbers, J + +;
4,8,3,2,1,9,6,7,5---------------Results 4,3,8,2,1,9,6,7,5
J J+1
Then J: 8 and j+1: 2 comparison, 8>2, large number in front, exchange two numbers, J + +;
4,3,8,2,1,9,6,7,5---------------Results 4,3,2,8,1,9,6,7,5
J J+1
Then J: 8 and j+1: 1 comparison, 8>1, large number in front, exchange two numbers, J + +;
4,3,2,8,1,9,6,7,5---------------Results 4,3,2,1,8,9,6,7,5
J J+1
Then J: 8 and j+1: 9 comparison, 8<9, large number in the rear, not moving, j + +;
4,3,2,1,8,9,6,7,5---------------Results 4,3,2,1,8,9,6,7,5
J J+1
Then J: 9 and j+1: 6 comparison, 9>6, large number in front, exchange two numbers, J + +;
4,3,2,1,8,9,6,7,5---------------Results 4,3,2,1,8,6,9,7,5
J J+1
Then J: 9 and j+1: 7 comparison, 9>7, large number in front, exchange two numbers, J + +;
4,3,2,1,8,6,9,7,5---------------Results 4,3,2,1,8,6,7,9,5
J J+1
Then J: 9 and j+1: 5 comparison, 9>5, large number in front, exchange two numbers, J + +;
4,3,2,1,8,6,7,9,5---------------Results 4,3,2,1,8,6,7,5,9
J J+1
Here the first round of sorting is done, and we see that the largest number is already at the end of the sequence, thus entering the second round of circulation (note:j<9-1-0=n-1-i; )
(2) Second round of circulation <i=1>:
J from the beginning, the first number of J: 4 and j+1: 3 comparison, 4>3, large number in front, exchange two numbers, J + +;
4,3,2,1,8,6,7,5,9---------------Results 3,4,2,1,8,6,7,5,9
J J+1
Repeat the steps in cycle one until:
3,2,1,4,6,7,8,5,9---------------Results 3,2,1,4,6,7,5,8,9
J J+1
Here the second round of sorting is done, and we see that the largest number is already at the end of the sequence, thus entering the third round of loops
We can see that J's number is smaller than the previous one because the last number is already lined up j<9-1-1=n-1-i;
(3) third round cycle <i=2>:
Results: 2,1,3,4,6,5,7,8,9
(4) fourth round circulation <i=3>:
Results: 1,2,3,4,5,6,7,8,9
(4) Fifth round circulation <i=4>:
Results: 1,2,3,4,5,6,7,8,9
(4) Sixth round circulation <i=5>:
Results:4,5,6,7,8,9 ,
(4) Seventh round circulation <i=6>:
Results:3,4,5,6,7,8,9
(4) eighth round circulation <i=7>:
Results: 1,2,3,4,5,6,7,8,9
Write here the whole bubble sort end
It is assumed that the sequence of n elements needs to be bubble-sorted, with an array representation of a[n]={4,8,3,2,1,9,6,7,5};
Here we only need a two-layer loop, one layer to control the first cycle (ie i),i<n-1;
The second layer is used to control the position of the comparison (i.e. J), J<n-1-i
The following code is given:
1#include <stdio.h>2 intMain () {3 inta[9] = {3,4,9,8,1,2,5,7,6 };4 for(intm =0; m<9; m++)5 {6 if(M! =8) {7printf"%d,", A[m]);8 }9 Else {Tenprintf"%d", A[m]); One } A } -printf"\ n"); - for(inti =0; I <9-1; i++) the for(intj =0; j<9-1I J + +) - { - if(A[j]>a[j +1]) { - inttemp; +temp =A[j]; -A[J] = a[j +1]; +A[j +1] =temp; A at } - } - for(intm =0; m<9; m++) - { - if(m!=8){ -printf"%d,", A[m]); in } - Else { toprintf"%d", A[m]); + } - the } * GetChar (); $}
Sort algorithm-----Bubble sort