Copy Code code as follows:
/**
* Principle:
* for n cycles, each cycle from the back forward to the adjacent two elements of the comparison, small forward, big back
*
* Time Complexity:
* Average condition: O (n^2)
* Best case: O (n)
* Worst case: O (n^2)
*
* Stability: Stable
**/
public class bubble sort {
public int[] Bubblesort (int[] A, int n) {
for (int i = 0; I < n; i++) {
int flag = 0;
for (int j = n-1 J > I j--) {//I or i-1?
if (A[j] < a[j-1]) {
int x = a[j];
A[j] = a[j-1];
a[j-1] = x;
flag = 1;
&NBsp; }
}
if (flag = 0)
break;
}
return A;
}
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Int[] A = new int[] {25, 56, 32, 20, 1, 5, 89, 3, 8, 41};
Bubble sort sort = new bubble sort ();
Sort.bubblesort (A, a.length);
for (int i = 0; i < a.length; i++) {
System.out.print (A[i] + "");
}
}
}