Bubble sort
1.Bubble sort
2.Procedures
3.Example code (Java)
public static void bubbleSort(int[] listToSort) {
for (int i = 0; i < listToSort.length; i++) {
booblean swapped = false;
for (int j = i + 1; j < listToSort.length; j++) {
if (listToSort[j] < listToSort[j - 1]) {
swap(listToSort, j, j - 1);
swapped = true;
}
}
print(listToSort);
if (!swapped) {
break;
}
}
}Last updated