0
0
DSA Goprogramming~20 mins

Bubble Sort Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bubble Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Bubble Sort on a small array
What is the output of the following Go code after performing bubble sort on the array?
DSA Go
package main
import "fmt"
func bubbleSort(arr []int) {
    n := len(arr)
    for i := 0; i < n-1; i++ {
        for j := 0; j < n-i-1; j++ {
            if arr[j] > arr[j+1] {
                arr[j], arr[j+1] = arr[j+1], arr[j]
            }
        }
    }
}
func main() {
    arr := []int{5, 1, 4, 2, 8}
    bubbleSort(arr)
    fmt.Println(arr)
}
A[1 2 4 5 8]
B[5 4 2 1 8]
C[8 5 4 2 1]
D[1 4 2 5 8]
Attempts:
2 left
💡 Hint
Bubble sort swaps adjacent elements if they are in the wrong order, repeatedly.
🧠 Conceptual
intermediate
1:00remaining
Number of passes in Bubble Sort
For an array of length n, how many passes does the bubble sort algorithm perform in the worst case?
An passes
Bn-1 passes
Cn/2 passes
Dn+1 passes
Attempts:
2 left
💡 Hint
Each pass places the largest unsorted element at the end.
🔧 Debug
advanced
2:00remaining
Identify the error in this Bubble Sort implementation
What error will this Go code produce when run?
DSA Go
package main
import "fmt"
func bubbleSort(arr []int) {
    n := len(arr)
    for i := 0; i < n; i++ {
        for j := 0; j < n-i-1; j++ {
            if arr[j] > arr[j+1] {
                arr[j], arr[j+1] = arr[j+1], arr[j]
            }
        }
    }
}
func main() {
    arr := []int{3, 2, 1}
    bubbleSort(arr)
    fmt.Println(arr)
}
Aruntime error: index out of range
B[1 2 3]
Cno output, infinite loop
Dcompilation error
Attempts:
2 left
💡 Hint
Check the inner loop boundary conditions carefully.
🚀 Application
advanced
1:30remaining
Optimizing Bubble Sort to stop early
Which modification to the bubble sort algorithm allows it to stop early if the array is already sorted?
AUse recursion instead of loops.
BReduce the array size by one after each pass without checking swaps.
CSort only half of the array elements.
DAdd a flag to check if any swaps happened in a pass; stop if none.
Attempts:
2 left
💡 Hint
If no swaps occur in a pass, the array is sorted.
Predict Output
expert
2:30remaining
Output of Bubble Sort on a reversed array with duplicates
What is the output of the following Go code after bubble sort is applied?
DSA Go
package main
import "fmt"
func bubbleSort(arr []int) {
    n := len(arr)
    for i := 0; i < n-1; i++ {
        for j := 0; j < n-i-1; j++ {
            if arr[j] > arr[j+1] {
                arr[j], arr[j+1] = arr[j+1], arr[j]
            }
        }
    }
}
func main() {
    arr := []int{9, 7, 7, 5, 3, 3, 1}
    bubbleSort(arr)
    fmt.Println(arr)
}
A[9 7 7 5 3 3 1]
B[3 3 5 7 7 9 1]
C[1 3 3 5 7 7 9]
D[1 3 5 7 9]
Attempts:
2 left
💡 Hint
Bubble sort sorts all elements including duplicates in ascending order.