Challenge - 5 Problems
Selection Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Selection Sort on a small array
What is the output of the following Java code after running selection sort on the array?
DSA Java
public class Main { public static void selectionSort(int[] arr) { 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; } } public static void main(String[] args) { int[] data = {64, 25, 12, 22, 11}; selectionSort(data); for (int num : data) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Selection sort finds the smallest element and puts it at the start, then repeats for the rest.
✗ Incorrect
Selection sort repeatedly selects the smallest remaining element and swaps it to the front. After sorting, the array is in ascending order: 11 12 22 25 64.
🧠 Conceptual
intermediateNumber of swaps in selection sort
How many swaps does selection sort perform on an array of length n in the worst case?
Attempts:
2 left
💡 Hint
Selection sort swaps only once per outer loop iteration.
✗ Incorrect
Selection sort performs exactly one swap per outer loop iteration, so for n elements, it does n-1 swaps in the worst case.
🔧 Debug
advancedIdentify the bug in this selection sort code
What error will this Java code cause when running selection sort?
DSA Java
public static void selectionSort(int[] arr) { for (int i = 0; i <= arr.length; 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
Check the loop condition for i carefully.
✗ Incorrect
The outer loop uses i <= arr.length which causes i to go out of array bounds, leading to ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedSelection sort on a reversed array
What is the output after running selection sort on this array?
DSA Java
public class Main { public static void selectionSort(int[] arr) { 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; } } public static void main(String[] args) { int[] data = {5, 4, 3, 2, 1}; selectionSort(data); for (int num : data) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Selection sort always sorts the array in ascending order.
✗ Incorrect
Selection sort finds the smallest element and places it at the start repeatedly, so the reversed array becomes sorted ascending: 1 2 3 4 5.
🧠 Conceptual
expertTime complexity of selection sort
What is the time complexity of selection sort in all cases (best, average, worst)?
Attempts:
2 left
💡 Hint
Selection sort always scans the entire unsorted part to find the minimum.
✗ Incorrect
Selection sort always does nested loops over the array, so it takes O(n^2) time no matter the input.
