Bubble Sort The basic idea of bubble sorting is to compare the size of adjacent two elements each time, and swap positions if the order is wrong.
For example, there are 5 numbers 12 35 99 18 76, to sort from large to small. So the smaller the farther back .
First, the size of the 1th and 2nd digits is compared. Since 12 is less than 35, they both swap positions. After Exchange: 99 18 76.
Then compare the size of the 2nd and 3rd bits. Since 12 is less than 99, they both swap positions. After Exchange: 18 76 .
then repeat the above steps to compare the 3rd and 4th digits, 4th and 5th bits. The order of the 5 numbers after four comparisons is: three . so we put the smallest number in place. Each number is returned to us as ' a trip '. The following is the repetition of the above process, the second small number of digits, the third small number of the place, so came to the fourth trip. Although it is a coincidence that the five numbers are in the right place, it is not necessary to change the data.
In the above example, it is found that the bubble sort principle is: each trip will be a number of digits . That is, the first trip can only place the number on the fifth position, the second trip can only return the second-to-last number, the third will be the number of the third place, and in the front there are two positions in the book did not return, but also need to carry out the fourth trip.
The first and second digits are simply compared, so that not only the second digit is determined, but the number one is determined.
To sum up: if there are n number need to sort, just n-1 the number of digits, that is, to carry out the n-1 trip operation. Each trip will need to start from the first two adjacent number of comparisons, according to the requirements (from the big to the small or small to large) to determine whether the position is correct, incorrect transposition. Until the last number that has not been returned. Note that the number that has been returned does not need to be compared again .
Check the code:
#include <stdio.h>intMain () {intn,a[ -],i,j,temp; scanf ("%d",&N); for(i=0; i<n;i++) scanf ("%d",&A[i]); for(i=0;i<n-1;i++)//just a n-1 trip comparison for(j=0;j<n-i-1; j + +)//each time from the first to start comparing, has been returned to the place no longer compare if(a[j]<a[j+1]) { temp=a[j] ;//sort from big to small, with small numbers in the backa[j]=a[j+1]; A[j+1]=temp; } for(i=0; i<n;i++) printf ("%d", A[i]);//Output Results return 0;}
This method can compensate for the problem of the previous section and add a structure.
1#include <stdio.h>2 structstudent{3 Charname[ +];4 intscore;5};//store student's name and score6 intMain () {7 structStudent a[ -],temp;8 intI,j,n;9scanf"%d",&n);Ten for(i=0; i<n;i++) Onescanf"%s%d",a[i].name,&a[i].score); A for(i=0; i<n-1; i++) - for(j=0; j<n-i-1; j + +) - if(a[j].score<a[j+1].score) {//sort from high to low, compare fractions thetemp=A[j]; -a[j]=a[j+1]; -a[j+1]=temp; - } + for(i=0; i<n;i++)//Output Name -printf"%s\n", a[i].name); + return 0; A}
The core of a bubbling sort is a double nested loop.
Ah ha! The first chapter of the algorithm the second section---bubble sort