The bubbling sort algorithm is the simplest of the sorting methods, but it is very slow to run, so the bubbling sort is a very good algorithm to start the sorting study.
Bubble sort after the first round of sorting (from small to large), although the data can not be sorted in order, but the largest data to the last, then we in the next few rounds, followed by the maximum value, until there is only one data, at this time, the data sorting is complete:
The following example:
6,12,16,7,5;
After the first round: 6,12,7,5,16; You only need to compare the first four data
After the second round: 6,7,5,12,16;
After the third round: 6,5,7,12,16;
After the fourth round: 5,6,7,12,16;
The sorting is complete at this time.
Java code
public static void Main (string[] args) {
int b[]={6,12,1,16,7,5,1};
Bubblesort (b);
for (int i=0;i<b.length;i++) {
System.out.print (b[i]+ "");
}
}
public static void Bubblesort (int []b) {
int temp=0;
for
(int i=b.length-1;i>0;i--) {
//for
(int j=0;j<i;j++) {
if (b[j]>b[j+1]) {
TEMP=B[J+1];
B[J+1]=B[J];
b[j]=temp;}}}}