Bubble sort (Bubble sort) is a sort of interchange, the basic idea : 22 compare the keywords of adjacent records, if the reverse order is exchanged, until there is no reverse sequence of records.
The following is the code for the implementation:
public class Bubblesort {public
static void (int arr[], int x, int y) {
int tmp = arr[x];
ARR[X] = Arr[y];
Arr[y] = tmp;
}
public static void Bubblesort (int arr[]) {
Boolean flag = true;
for (int i=0;i<arr.length && flag;i++) { //If flag is true, exit loop
flag = false;
for (int j=arr.length-2;j>=i;j--) { //j from back-forward loop
if (Arr[j] > Arr[j+1]) { //the previous one is greater than
the last swap (arr, J, j+1); The value of the Exchange Arr[j] and arr[j+1]
flag = true; If there is an Exchange flag set to True}}}
Test:
public static void Main (string[] args) {
//TODO auto-generated method stub
int a[] = {2, 5, 7, 4, 1, 9, 0, 3, 6 , 8};
Bubblesort (a);
for (int i=0;i<a.length;i++) {
System.out.print (a[i]+ "");
}
System.out.println ();
}
Results: