Bubble sort is a sort algorithm, clear thinking, code concise, often used in college students computer courses.
The name "bubbling" comes from the fact that the larger element will slowly "float" through the exchange to the top of the sequence, hence the name.
This is explained by the example of small to large sort.
Basic ideas and illustrative examples
The basic idea of bubble sorting is to keep comparing the next two numbers, allowing the larger elements to move backwards. After a comparison, the largest number is chosen, and after the 2nd round, the number of the second largest is chosen, and so on.
The following is a bubble sort description for 3 2 4 1.
First round sorting process
3 2 4 1 (initial)
2 3 4 2 (compare 3 and 2, swap)
2 3 4 1 (comparison 3 and 4, not exchanged)
2 3 1 4 (compare 4 and 1, swap)
At the end of the first round, the largest number 4 is already on the last side, so the second round of sorting only requires a comparison of the previous three numbers.
Second round sorting process
2 3 1 4 (First round sorting results)
2 3 1 4 (Comparison 2 and 3, not exchanged)
2 1 3 4 (compare 3 and 1, swap
At the end of the second round, the second-largest number is in the second-to-last position, so the third round only needs to compare the first two elements.
Third round sequencing process
2 1 3 4 (second round sorting results)
1 2 3 4 (compare 2 and 1, swap)
At this point, the sort ends.
Algorithm Summary and implementation
For arrays with n elements R[n], up to N-1 wheel comparison;
The first round, by comparison (R[1], r[2]), (r[2], r[3]), (R[3], r[4]), ... (R[n-1], r[n]); The largest element is moved to R[n].
The second round, comparison (r[1], r[2]), (r[2], r[3]), (R[3], r[4]), ... (R[n-2], r[n-1]); The second largest element is moved to r[n-1].
。。。。
And so on, until the entire array is sorted from small to large.
The general implementation and optimization of bubble sort are given below. General implementation is a common method of implementation in textbooks, regardless of whether the array is sorted well, will be N-1 wheel comparison, and the optimization of the implementation, in the case of the array has been sorted, will be early exit comparison, reducing the time complexity of the algorithm.
1#include <stdio.h>2#include <stdlib.h>3 #defineN 84 voidBubble_sort (intA[],intn);5 //General Implementation6 voidBubble_sort (intA[],intN//n number of elements for array a7 {8 //must be N-1 wheel comparison9 for(intI=0; i<n-1; i++)Ten { One //each round is compared before the n-1-i, that is, the last I of the sorted well without comparison A for(intj=0; j<n-1-I.; J + +) - { - if(A[j] > a[j+1]) the { - inttemp =A[j]; -A[J] = a[j+1]; -a[j+1]=temp; + } - } + } A } at //Optimized Implementation - voidBubble_sort_better (intA[],intN//n number of elements for array a - { - //up to N-1 wheel comparison - for(intI=0; i<n-1; i++) - { in BOOLissorted =true; - //each round is compared before the n-1-i, that is, the last I of the sorted well without comparison to for(intj=0; j<n-1-I.; J + +) + { - if(A[j] > a[j+1]) the { *issorted =false; $ inttemp =A[j];Panax NotoginsengA[J] = a[j+1]; -a[j+1]=temp; the } + } A if(issorted) Break;//If no swap occurs, the array is sorted. the } + } - intMain () $ { $ intNum[n] = { the, -, One, +, the, -, +, -}; -Bubble_sort (num, N);//or use Bubble_sort_better (num, N); - for(intI=0; i<n; i++) theprintf"%d", Num[i]); -printf"\ n");WuyiSystem"Pause"); the return 0; -}
Algorithm sorting algorithm--bubble sort