Complete the code to start the outer loop for selection sort.
for i in 0..[1] { // find the minimum element in unsorted array }
The outer loop runs from 0 to length-1 because the last element will be sorted by then.
Complete the code to start the inner loop to find the minimum element.
for j in [1]..arr.len() { if arr[j] < arr[min_index] { min_index = j; } }
The inner loop starts from the element right after the current outer loop index to find the minimum in the unsorted part.
Fix the error in swapping the elements after finding the minimum.
let temp = arr[i];
arr[i] = arr[[1]];
arr[min_index] = temp;We swap the element at i with the element at min_index, which is the minimum found.
Fill both blanks to complete the selection sort function header and initialize min_index.
fn selection_sort(arr: &mut Vec<i32>) {
for i in 0..[1] {
let mut min_index = [2];
// rest of the code
}
}The outer loop runs to length-1 and min_index starts at i for each iteration.
Fill all three blanks to complete the selection sort inner loop and swap logic.
for j in [1]..arr.len() { if arr[j] [2] arr[min_index] { min_index = j; } } if min_index != i { let temp = arr[i]; arr[i] = arr[[3]]; arr[min_index] = temp; }
The inner loop starts at i+1, compares if arr[j] is less than arr[min_index], and swaps arr[i] with arr[min_index].
