![]() ![]() ![]() |
To embed a source file containing Java code:
public class BubbleSort { // Class BubbleSort
public static void sort(int[] a) { // static method sort
int tmp; // variable for exchange
boolean swapped; // true, if a swap has been made
do {
swapped = false; // not yet swaped
for (int i=0; i<a.length-1; i++){ // run through array
if (a[i] > a[i+1]) { // if neighbors are in wrong relation
tmp = a[i]; // store
a[i] = a[i+1]; // both elements
a[i+1] = tmp; // in correct order
swapped = true; // make a note of the swap
}
}
} while (swapped); // as long as being swaped
}
}
public class BubbleSort { // Class BubbleSort public static void sort(int[] a) { // static method sort int tmp; // variable for exchange boolean swapped; // true, if a swap has been made do { swapped = false; // not yet swaped for (int i=0; i<a.length-1; i++){ // run through array if (a[i] > a[i+1]) { // if neighbors are in wrong relation tmp = a[i]; // store a[i] = a[i+1]; // both elements a[i+1] = tmp; // in correct order swapped = true; // make a note of the swap } } } while (swapped); // as long as being swaped } } |