The idea of bubbling is: small, nonstop "running forward", large and constant "backwards" .
The code is as follows:
public class Main {public static void Main (string[] args) { int a[]={7,8,1,3,5}; System.out.println ("Before sorting:"); Print (a); Bubblesort (a); System.out.println (); System.out.println ("After sorting:"); Print (a); } public static void Bubblesort (int a[]) { int temp=0; int i; for (i=0;i<a.length;i++) {for (int j=a.length-1;j>i;j--) { if (A[j]<a[j-1]) { temp=a[j]; A[J]=A[J-1]; A[j-1]=temp ; }}}} static void print (int []a) { for (int s:a) System.out.print (s+ ","); } }
Each iteration of the cyclic variable I determines a minimum number from the I-bit (including the I-bit) to the last. The final iteration is finished with I and the sorting is done.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Authentic Bubbling Method-java language implementation