- Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
- Getting each element one by one and comparing it with adjacent elements and swapping the positions.
public void bubbleSort() {
int temp, firstValue, secondValue;
int[] arr = { 1, 90, 25, 22, 4, 50 };
printArray(arr);
for (int i = 0; i < arr.length; i++) {
// #arr.length - 2 -- avoids next index of last index -- secondValue
for (int j = 0; j < arr.length - 2; j++) {
firstValue = arr[j];
secondValue = arr[j + 1];
if ((firstValue > secondValue)) {
temp = firstValue;
arr[j] = secondValue;
arr[j + 1] = temp;
}
}
}
System.out.println("-----SORTING DONE-----");
printArray(arr);
}
Reference Link https://www.youtube.com/watch?v=T8ErAYobcbc
matrix
No comments:
Post a Comment