Complete the code to swap two elements in the array.
int temp = arr[j]; arr[j] = arr[[1]]; arr[[1]] = temp;
In bubble sort, we swap the current element arr[j] with the next element arr[j + 1] when they are out of order.
Complete the code to compare two adjacent elements in the array.
if (arr[j] > arr[[1]]) { // swap elements }
We compare the current element arr[j] with the next element arr[j + 1] to decide if a swap is needed.
Fix the error in the loop condition to avoid out-of-bound errors.
for (int j = 0; j < arr.length [1] 1; j++) { // inner loop body }
The inner loop should run until arr.length - 1 to avoid accessing beyond the array size.
Fill both blanks to complete the nested loops for bubble sort.
for (int [1] = 0; [1] < arr.length - 1; [1]++) { for (int [2] = 0; [2] < arr.length - 1 - [1]; [2]++) { // compare and swap } }
The outer loop uses i to track passes, and the inner loop uses j to compare adjacent elements.
Fill all three blanks to complete the swap and comparison in bubble sort.
if (arr[[1]] > arr[[2]]) { int temp = arr[[3]]; arr[[1]] = arr[[2]]; arr[[2]] = temp; }
i instead of j in the inner loop.We compare arr[j] and arr[j + 1], then swap these two elements if needed.
