Challenge - 5 Problems
Selection Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Selection Sort on a small list
What is the output of the following selection sort code on the list [3, 1, 4, 2]?
DSA Python
def selection_sort(arr): n = len(arr) for i in range(n): min_idx = i for j in range(i+1, n): if arr[j] < arr[min_idx]: min_idx = j arr[i], arr[min_idx] = arr[min_idx], arr[i] lst = [3, 1, 4, 2] selection_sort(lst) print(lst)
Attempts:
2 left
💡 Hint
Selection sort finds the smallest element and swaps it to the front in each pass.
✗ Incorrect
Selection sort repeatedly selects the smallest remaining element and places it at the current position, resulting in a sorted list.
🧠 Conceptual
intermediateNumber of swaps in Selection Sort
How many swaps does selection sort perform on a list 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 iteration of the outer loop, so total swaps are n - 1.
🔧 Debug
advancedIdentify the error in this selection sort implementation
What error will this code produce when run on the list [5, 2, 9]?
DSA Python
def selection_sort(arr): n = len(arr) for i in range(n): min_idx = i for j in range(i+1, n): if arr[j] > arr[min_idx]: min_idx = j arr[i], arr[min_idx] = arr[min_idx], arr[i] lst = [5, 2, 9] selection_sort(lst) print(lst)
Attempts:
2 left
💡 Hint
Check the comparison operator in the inner loop.
✗ Incorrect
The code uses '>' instead of '<', so it finds the maximum element each time, sorting the list in descending order.
❓ Predict Output
advancedSelection Sort on a list with duplicates
What is the output after running selection sort on the list [4, 2, 4, 3, 2]?
DSA Python
def selection_sort(arr): n = len(arr) for i in range(n): min_idx = i for j in range(i+1, n): if arr[j] < arr[min_idx]: min_idx = j arr[i], arr[min_idx] = arr[min_idx], arr[i] lst = [4, 2, 4, 3, 2] selection_sort(lst) print(lst)
Attempts:
2 left
💡 Hint
Selection sort handles duplicates by placing equal elements in order found.
✗ Incorrect
Selection sort sorts the list including duplicates, resulting in [2, 2, 3, 4, 4].
🧠 Conceptual
expertTime complexity of selection sort in best and worst cases
What is the time complexity of selection sort in both best and worst cases?
Attempts:
2 left
💡 Hint
Selection sort always scans the entire unsorted part to find the minimum.
✗ Incorrect
Selection sort always performs n*(n-1)/2 comparisons regardless of input order, so both best and worst cases are O(n^2).
