Complete the code to swap two elements in the array.
int temp = arr[[1]]; arr[[1]] = arr[i]; arr[i] = temp;
The variable minIndex holds the index of the smallest element found. We swap the element at minIndex with the element at i.
Complete the code to find the index of the smallest element in the unsorted part of the array.
if (arr[j] [1] arr[minIndex]) { minIndex = j; }
We want to find the smallest element, so we check if arr[j] is less than arr[minIndex].
Fix the error in the for-loop that iterates over the unsorted part of the array.
for (int j = i + 1; j [1] arr.Length; j++) { // find the smallest element }
The loop should run while j is less than arr.Length to avoid going out of bounds.
Fill both blanks to complete the outer loop and initialize the minimum index.
for (int [1] = 0; i [2] arr.Length - 1; i++) { int minIndex = i; // inner loop to find smallest element }
The outer loop uses i starting at 0 and runs while i < arr.Length - 1 because the last element will be sorted by then.
Fill all three blanks to complete the selection sort method.
public void SelectionSort(int[] arr) {
for (int [1] = 0; i [2] arr.Length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j [3] arr.Length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}The outer loop variable is i. It runs while i < arr.Length - 1. The inner loop variable j runs while j < arr.Length.
