What is Selection Sort?
A simple sorting algorithm that repeatedly selects the smallest element and swaps it to the correct position.
What is the first step of Selection Sort?
Finding the smallest element in the array.
What does the outer loop in Selection Sort represent?
It represents the sorted portion of the array.
What does Selection Sort primarily do in each step?
It finds the smallest element in the unsorted part of the array and swaps it to its correct position.
What does the algorithm do after finding the smallest element?
It swaps the smallest element with the first unsorted element.
What does the inner loop in Selection Sort do?
It finds the smallest element in the unsorted portion of the array.
What type of sorting algorithm is Selection Sort?
It is a comparison-based, in-place sorting algorithm.
How many times does Selection Sort swap elements in a fully sorted array?
Zero times because no swaps are needed.
What will the array {7, 3, 5, 2} look like after one pass of Selection Sort?
{2, 3, 5, 7} (The smallest element 2 is swapped to the first position.)
In what type of data structures does Selection Sort work best?
It works best with arrays but is inefficient for linked lists.
How does Selection Sort handle duplicate values?
It still sorts them correctly, but it is not stable, so equal elements may change order.
What will happen if you forget to swap elements in Selection Sort?
The array will remain unsorted.
Does Selection Sort require extra memory to sort an array?
No, it is an in-place sorting algorithm.
What happens in the final pass of Selection Sort?
The second-to-last element is compared with the last element, and a final swap is performed if needed.
Given this Java snippet, what is missing?
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
// ???
}
The swap operation
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;