What is the main idea behind the Selection Sort algorithm?
Selection Sort repeatedly finds the smallest element from the unsorted part and swaps it with the first unsorted element, growing the sorted part step by step.
Click to reveal answer
intermediate
In Selection Sort, how many times do we swap elements in the worst case?
Selection Sort swaps elements exactly (n-1) times, where n is the number of elements, because it swaps once per pass.
Click to reveal answer
beginner
Show the state of the array after the first pass of Selection Sort on [29, 10, 14, 37, 13].
After first pass, the smallest element 10 is swapped with 29: [10, 29, 14, 37, 13]
Click to reveal answer
intermediate
What is the time complexity of Selection Sort and why?
Selection Sort has time complexity O(n²) because it uses two nested loops: one to select each element and one to find the minimum in the unsorted part.
Click to reveal answer
intermediate
Why is Selection Sort not efficient for large datasets?
Because Selection Sort always does n² comparisons regardless of the initial order, making it slow for large lists compared to faster algorithms like Quick Sort or Merge Sort.
Click to reveal answer
What does Selection Sort do in each pass?
AFinds the smallest element and swaps it with the first unsorted element
BSwaps adjacent elements if they are in wrong order
CDivides the list into halves and sorts each half
DUses a pivot to partition the list
✗ Incorrect
Selection Sort finds the smallest element in the unsorted part and swaps it with the first unsorted element in each pass.
How many passes does Selection Sort make for an array of size n?
An
Blog n
Cn/2
Dn-1
✗ Incorrect
Selection Sort makes n-1 passes because after sorting n-1 elements, the last element is already in place.
What is the best case time complexity of Selection Sort?
AO(n²)
BO(n log n)
CO(n)
DO(log n)
✗ Incorrect
Selection Sort always performs O(n²) comparisons regardless of input order.
Which of these is true about Selection Sort swaps?
AIt swaps elements multiple times per pass
BIt swaps only once per pass
CIt never swaps elements
DIt swaps only if the array is already sorted
✗ Incorrect
Selection Sort swaps the smallest found element with the first unsorted element once per pass.
Which sorting algorithm is generally faster than Selection Sort for large datasets?
ABubble Sort
BInsertion Sort
CQuick Sort
DSelection Sort
✗ Incorrect
Quick Sort is generally faster with average time complexity O(n log n), better than Selection Sort's O(n²).
Explain how Selection Sort works step-by-step on a small array.
Think about how you would sort playing cards by picking the smallest card each time.
You got /4 concepts.
Describe the advantages and disadvantages of Selection Sort.
Consider when you might want to use it despite its slowness.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Selection Sort algorithm?
easy
A. Repeatedly find the smallest element and move it to the front
B. Divide the list into halves and merge sorted halves
C. Swap adjacent elements if they are in wrong order repeatedly
D. Use a pivot to partition the list into smaller parts
Solution
Step 1: Understand selection sort process
Selection sort finds the smallest element in the unsorted part and swaps it with the first unsorted element.
Step 2: Compare with other sorting methods
Other options describe merge sort, bubble sort, and quicksort, which are different algorithms.
Final Answer:
Repeatedly find the smallest element and move it to the front -> Option A
Quick Check:
Selection sort = find smallest and swap [OK]
Hint: Selection sort always picks the smallest next element [OK]
Common Mistakes:
Confusing selection sort with bubble sort
Thinking it divides list like merge sort
Assuming it uses a pivot like quicksort
2. Which of the following is the correct way to start the outer loop for selection sort in Go for an array arr of length n?
easy
A. for i := 1; i < n; i++ {
B. for i := 0; i < n-1; i++ {
C. for i := 0; i <= n; i++ {
D. for i := 1; i <= n; i++ {
Solution
Step 1: Understand loop range for selection sort
The outer loop runs from 0 to n-2 because the last element will be sorted automatically after previous passes.
Step 2: Check each option's correctness
for i := 0; i < n-1; i++ { uses i < n-1, which is correct. Others either go out of bounds or start at wrong index.
Final Answer:
for i := 0; i < n-1; i++ { -> Option B
Quick Check:
Outer loop runs 0 to n-2 [OK]
Hint: Outer loop stops at n-2 because last element is sorted [OK]
Common Mistakes:
Using <= n causing index out of range
Starting loop at 1 instead of 0
Loop running too many times
3. What will be the output of the following Go code snippet after applying one full pass of selection sort on the array?
Minimum element is 10 at index 1, so minIndex = 1.
Step 2: Swap arr[0] and arr[minIndex]
Swap arr[0] (29) with arr[1] (10), resulting in [10 29 14 37 13].
Final Answer:
[10 29 14 37 13] -> Option D
Quick Check:
First pass places smallest element at front [OK]
Hint: First pass swaps smallest element to front [OK]
Common Mistakes:
Not swapping elements after finding minimum
Confusing minIndex with current index
Printing array before swap
4. Identify the error in the following Go code implementing selection sort:
func selectionSort(arr []int) {
n := len(arr)
for i := 0; i < n; i++ {
minIndex := i
for j := i + 1; j <= n; j++ {
if arr[j] < arr[minIndex] {
minIndex = j
}
}
arr[i], arr[minIndex] = arr[minIndex], arr[i]
}
}
medium
A. Swap should be done only if minIndex != i
B. minIndex should be initialized outside the outer loop
C. Inner loop condition should be j < n, not j <= n
D. Outer loop should run from i := 1 to n-1
Solution
Step 1: Check inner loop boundary
Inner loop uses j <= n, which causes index out of range error because valid indices are 0 to n-1.
Step 2: Correct inner loop condition
It should be j < n to stay within array bounds.
Final Answer:
Inner loop condition should be j < n, not j <= n -> Option C
Quick Check:
Array indices go from 0 to n-1 [OK]
Hint: Array indices end at n-1, not n [OK]
Common Mistakes:
Using <= n causing runtime panic
Initializing minIndex outside outer loop
Unnecessary swap checks
5. Given the array arr := []int{64, 25, 12, 22, 11}, after how many swaps will the array be fully sorted using selection sort, and what will be the final sorted array?
hard
A. 4 swaps; [11 12 22 25 64]
B. 5 swaps; [11 12 22 25 64]
C. 4 swaps; [64 25 12 22 11]
D. 3 swaps; [11 12 22 25 64]
Solution
Step 1: Count swaps in each selection sort pass
Pass 1: swap 64 and 11 -> [11 25 12 22 64] Pass 2: swap 25 and 12 -> [11 12 25 22 64] Pass 3: swap 25 and 22 -> [11 12 22 25 64] Pass 4: swap 25 and 25 (no change, but swap counted as 1) Total swaps = 4 (last swap is with itself, usually counted as swap)
Step 2: Final sorted array
The array after sorting is [11 12 22 25 64].
Final Answer:
4 swaps; [11 12 22 25 64] -> Option A
Quick Check:
Selection sort swaps once per pass, total n-1 swaps [OK]
Hint: Selection sort swaps once per pass, total n-1 swaps [OK]
Common Mistakes:
Counting swaps incorrectly when swapping element with itself
Assuming more swaps than passes
Confusing sorted array with original
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.