0
0
DSA Goprogramming~10 mins

Bubble Sort Algorithm in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap two elements in the slice.

DSA Go
arr[i], arr[i+1] = arr[i+1], arr[[1]]
Drag options to blanks, or click blank then click option'
Ai+1
Bi
Ci-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index like i+1 or i-1 in the swap.
Forgetting that both sides of the assignment must match the swapped elements.
2fill in blank
medium

Complete the code to run the inner loop for bubble sort.

DSA Go
for j := 0; j < len(arr) - 1 - [1]; j++ {
Drag options to blanks, or click blank then click option'
Alen(arr)
Bj
Ci
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i in the subtraction.
Not subtracting anything, causing unnecessary comparisons.
3fill in blank
hard

Fix the error in the comparison to swap elements if needed.

DSA Go
if arr[j] [1] arr[j+1] {
Drag options to blanks, or click blank then click option'
A!=
B<
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which would sort in descending order.
Using '==' or '!=' which do not help sorting.
4fill in blank
hard

Fill both blanks to complete the outer loop and initialize the slice length.

DSA Go
n := len(arr)
for [1] := 0; [2] < n-1; [2]++ {
Drag options to blanks, or click blank then click option'
Ai
Bj
Cn
Dlen(arr)
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop control.
Using 'j' which is usually for inner loop.
5fill in blank
hard

Fill all three blanks to complete the bubble sort function.

DSA Go
func bubbleSort(arr []int) {
    n := len(arr)
    for [1] := 0; [1] < n-1; [1]++ {
        for [2] := 0; [2] < n-1-[1]; [2]++ {
            if arr[[2]] [3] arr[[2]+1] {
                arr[[2]], arr[[2]+1] = arr[[2]+1], arr[[2]]
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
Ai
Bj
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing loop variable names.
Using wrong comparison operator.