0
0
DSA Goprogramming~20 mins

Selection Sort Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Selection Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Selection Sort on a small array
What is the output of the following Go code after running selection sort on the array?
DSA Go
package main
import "fmt"
func selectionSort(arr []int) {
    n := len(arr)
    for i := 0; i < n-1; i++ {
        minIdx := i
        for j := i + 1; j < n; j++ {
            if arr[j] < arr[minIdx] {
                minIdx = j
            }
        }
        arr[i], arr[minIdx] = arr[minIdx], arr[i]
    }
}
func main() {
    arr := []int{64, 25, 12, 22, 11}
    selectionSort(arr)
    fmt.Println(arr)
}
A[11 22 12 25 64]
B[64 25 12 22 11]
C[11 12 22 25 64]
D[25 12 22 11 64]
Attempts:
2 left
💡 Hint
Selection sort finds the smallest element and puts it at the start, then repeats for the rest.
🧠 Conceptual
intermediate
1:30remaining
Number of swaps in Selection Sort
How many swaps does selection sort perform on an array of length n in the worst case?
A0 swaps
Bn * (n - 1) / 2 swaps
Cn swaps
Dn - 1 swaps
Attempts:
2 left
💡 Hint
Selection sort swaps only once per outer loop iteration.
🔧 Debug
advanced
2:00remaining
Identify the error in this selection sort implementation
What error will this Go code produce when running selection sort?
DSA Go
package main
import "fmt"
func selectionSort(arr []int) {
    n := len(arr)
    for i := 0; i <= n-1; i++ {
        minIdx := i
        for j := i + 1; j < n; j++ {
            if arr[j] < arr[minIdx] {
                minIdx = j
            }
        }
        arr[i], arr[minIdx] = arr[minIdx], arr[i]
    }
}
func main() {
    arr := []int{3, 1, 2}
    selectionSort(arr)
    fmt.Println(arr)
}
A[1 2 3]
BIndex out of range runtime error
CCompilation error: missing import
D[3 1 2]
Attempts:
2 left
💡 Hint
Check the loop conditions carefully.
Predict Output
advanced
2:00remaining
Selection Sort on a reversed array
What is the output of the following Go code after sorting the reversed array?
DSA Go
package main
import "fmt"
func selectionSort(arr []int) {
    n := len(arr)
    for i := 0; i < n-1; i++ {
        minIdx := i
        for j := i + 1; j < n; j++ {
            if arr[j] < arr[minIdx] {
                minIdx = j
            }
        }
        arr[i], arr[minIdx] = arr[minIdx], arr[i]
    }
}
func main() {
    arr := []int{5, 4, 3, 2, 1}
    selectionSort(arr)
    fmt.Println(arr)
}
A[1 3 2 4 5]
B[1 2 3 4 5]
C[5 4 3 2 1]
D[2 1 3 4 5]
Attempts:
2 left
💡 Hint
Selection sort always sorts ascending regardless of input order.
🧠 Conceptual
expert
1:30remaining
Time complexity of Selection Sort
What is the time complexity of selection sort in all cases?
AO(n^2) in best, average, and worst cases
BO(n log n) in best case, O(n^2) in worst case
CO(n) in best case, O(n^2) in worst case
DO(n) in all cases
Attempts:
2 left
💡 Hint
Selection sort always scans the entire unsorted part to find the minimum.