Bubble/' b?b (?) l/n Bubble Foam v makes the bubbling swap/sw?p/n exchange of objects V with .... Exchange Trade Exchange
Bubble sort two ideas (primarily for arrays): two-step
1) Query minimum value (the minimum value of the first position is queried)
for (int j=0; j<a.length; J + +) {
if (A[0]>a[i]) {
Swap (0, i);
}
}
public void Swap (int i, int j) {
int tmp = A[i];
int min = a[j];
int a[j] = tmp;
}
2) for loop (traverse the minimum value of all positions sequentially
The outer for loop queries all locations (i's last traversal is not necessary because i==a.length-1 time, j==a.length-1 does not loop so i<a.length-1)
for (int i=0; i<a.length-1; i++) {
Inner for loop is the minimum value of the query
for (int j=i+1; j<a.length; J + +) {
if (A[i]<a[j]) {
Swap (I,J)
}
}
}
for (int i=0;i<length-1;i++) {
Inner loop is the maximum value on the last side
for (int j=0;j<length-i-1;j++) { if (a[j]>a[j+1]) swap (j,j+1); }
}
Sample transfer from other blogs
Class array{
int [] A;
int length;
Public Array (int max) {
Int[] A = new Int[max];
length=0;
}
public void Insert (int value) {
A[length] = value;
}
Public String display (int[] a) {
for (int i=0; i<length; i++) {
System.out.println ("a[" +i+ "] =" +i);
}
}
}
Bubble sort (bubble sort)---go to xingoo blog