0
0
DSA Goprogramming~30 mins

Bubble Sort Algorithm in DSA Go - Build from Scratch

Choose your learning style9 modes available
Bubble Sort Algorithm
📖 Scenario: You are helping a librarian organize a small shelf of books by their ID numbers. The books are currently in a random order, and you want to arrange them from the smallest ID to the largest using a simple sorting method.
🎯 Goal: Build a program that sorts a list of book IDs using the Bubble Sort algorithm. You will create the list, set up a variable to track the number of passes, implement the sorting logic, and finally print the sorted list.
📋 What You'll Learn
Create a slice of integers called bookIDs with the exact values: 34, 12, 25, 9, 56
Create an integer variable called n that stores the length of bookIDs
Use nested for loops with variables i and j to implement the Bubble Sort algorithm
Swap adjacent elements in bookIDs if they are in the wrong order
Print the sorted bookIDs slice
💡 Why This Matters
🌍 Real World
Sorting is a common task in organizing data, like arranging books, files, or records in order.
💼 Career
Understanding sorting algorithms like Bubble Sort helps in optimizing data processing and is a foundation for learning more advanced algorithms.
Progress0 / 4 steps
1
Create the list of book IDs
Create a slice of integers called bookIDs with these exact values: 34, 12, 25, 9, 56
DSA Go
Hint

Use bookIDs := []int{34, 12, 25, 9, 56} to create the slice.

2
Set up the length variable
Create an integer variable called n that stores the length of the bookIDs slice
DSA Go
Hint

Use len(bookIDs) to get the length of the slice.

3
Implement the Bubble Sort logic
Use nested for loops with variables i and j to implement the Bubble Sort algorithm. Swap adjacent elements in bookIDs if the element at j is greater than the element at j+1
DSA Go
Hint

Use two for loops to compare and swap adjacent elements.

4
Print the sorted list
Print the sorted bookIDs slice using fmt.Println(bookIDs). Remember to import the fmt package at the top.
DSA Go
Hint

Use fmt.Println(bookIDs) to print the sorted slice.