Complete the code to swap two elements in a list.
temp = arr[[1]] arr[[1]] = arr[[2]] arr[[2]] = temp
To swap elements at positions i and j, you first save the value at i, then assign the value at j to i, and finally assign the saved value to j.
Complete the code to compare two adjacent elements in bubble sort.
if arr[[1]] > arr[[2]]:
In bubble sort, you compare element at position j with the next element j+1 to decide if they need to be swapped.
Fix the error in the selection sort inner loop condition.
for j in range(i+1, [1]):
len(arr)-1 which skips last elementThe inner loop in selection sort runs from i+1 to the end of the list, which is len(arr). Using len(arr)-1 would miss the last element.
Fill both blanks to complete the bubble sort outer and inner loops.
for i in range([1]): for j in range([2] - i - 1):
len(arr) - 1 for outer loopThe outer loop runs from 0 to len(arr). The inner loop runs from 0 to len(arr) - i - 1 to avoid checking already sorted elements at the end.
Fill all three blanks to complete the selection sort swap step.
min_idx = [1] for j in range(i+1, [2]): if arr[j] < arr[min_idx]: min_idx = j arr[[3]], arr[i] = arr[i], arr[[3]]
In selection sort, you start with min_idx = i. The inner loop runs to len(arr). After finding the minimum index, you swap arr[min_idx] with arr[i].