Challenge - 5 Problems
Selection Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateWhat is the output of the selection sort on this array?
Given the array
[29, 10, 14, 37, 13], what will be the state of the array after the first full pass of selection sort?DSA C#
int[] arr = {29, 10, 14, 37, 13}; // After first pass of selection sort
Attempts:
2 left
š” Hint
Selection sort finds the smallest element and swaps it with the first unsorted element.
ā Incorrect
In the first pass, selection sort finds the smallest element (10) and swaps it with the first element (29). So the array becomes [10, 29, 14, 37, 13].
ā Predict Output
intermediateFinal sorted array after selection sort
What is the final sorted array after applying selection sort on
[64, 25, 12, 22, 11]?DSA C#
int[] arr = {64, 25, 12, 22, 11}; // After full selection sort
Attempts:
2 left
š” Hint
Selection sort sorts the array in ascending order by repeatedly selecting the smallest element.
ā Incorrect
Selection sort will reorder the array to [11, 12, 22, 25, 64] after all passes.
š§ Conceptual
advancedWhy does selection sort have O(n²) time complexity?
Selection sort has a time complexity of O(n²). Which reason best explains why?
Attempts:
2 left
š” Hint
Think about how many comparisons happen in each pass.
ā Incorrect
Selection sort compares each element with all others to find the minimum, leading to roughly n*(n-1)/2 comparisons, which is O(n²).
š§ Debug
advancedIdentify the bug in this selection sort code snippet
What is the bug in the following C# selection sort code snippet?
DSA C#
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; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; }
Attempts:
2 left
š” Hint
Selection sort finds the smallest element in the unsorted part.
ā Incorrect
The code uses '>' which finds the maximum element instead of the minimum. It should use '<' to find the minimum element for sorting in ascending order.
š Application
expertHow many swaps does selection sort perform on a sorted array?
If selection sort is applied to an already sorted array of 5 elements, how many swaps will it perform?
Attempts:
2 left
š” Hint
Selection sort swaps the minimum element with the current position in each pass, even if they are the same.
ā Incorrect
Selection sort performs one swap per pass except possibly the last. For 5 elements, it performs 4 swaps even if the array is sorted.
