Sorting algorithm that compares one element to the element to the right of it, and if it is bigger than it, the two will swap. Keeps running until all the elements are sorted.
public static void bubbleSort(int[]arr){
boolean swap = true;
int sorts = 1;
for(int i = arr.length - 1 ; i > 0 && swap; i--) {
swap = false;
for(int j = 0; j < i; j++){
if(arr[j] > arr[j+1]) { //if a swap needs to be done
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swap = true;
}
}
if(swap) { //if a swap has been done, then print
System.out.print("Sort " + sorts + ": ");
for(int k = 0; k<arr.length; k++){
System.out.print(arr[k] + "\\t");
}
System.out.println();
sorts++;
}
}
}