Introduction
Bubble Sort is a simple comparison-based sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. This guide will demonstrate how to write a Go program that sorts an array using the Bubble Sort algorithm.
Problem Statement
Create a Go program that:
- Prompts the user to enter the number of elements in an array.
- Takes input for the elements of the array.
- Sorts the array using the Bubble Sort algorithm.
- Displays the sorted array.
Example:
- Input: Array elements: [5, 3, 8, 4, 2]
- Output: Sorted array: [2, 3, 4, 5, 8]
Solution Steps
- Import the fmt Package: Use import "fmt"to include the fmt package for formatted I/O operations.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Input the Number of Elements: Use fmt.Scanlnto take input from the user for the number of elements in the array.
- Input the Array Elements: Use a loop to input the elements of the array from the user.
- Sort the Array Using Bubble Sort: Implement the Bubble Sort algorithm to sort the array.
- Display the Sorted Array: Use fmt.Printlnto display the sorted array.
Go Program
package main
import "fmt"
/**
 * Go Program to Sort an Array Using Bubble Sort
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the number of elements in the array
    var n int
    // Step 2: Prompt the user to enter the number of elements
    fmt.Print("Enter the number of elements in the array: ")
    fmt.Scanln(&n)
    // Step 3: Declare an array to hold the elements
    array := make([]int, n)
    // Step 4: Input the elements of the array
    fmt.Println("Enter the elements of the array:")
    for i := 0; i < n; i++ {
        fmt.Scanln(&array[i])
    }
    // Step 5: Sort the array using Bubble Sort
    for i := 0; i < n-1; i++ {
        for j := 0; j < n-i-1; j++ {
            if array[j] > array[j+1] {
                // Swap array[j] and array[j+1]
                array[j], array[j+1] = array[j+1], array[j]
            }
        }
    }
    // Step 6: Display the sorted array
    fmt.Println("Sorted array:", array)
}
Explanation
Step 1: Declare Variables
- The variable nis declared to store the number of elements in the array.
Step 2: Input the Number of Elements
- The program prompts the user to enter the number of elements using fmt.Print. Thefmt.Scanlnfunction reads the input and stores it in thenvariable.
Step 3: Declare the Array
- An array arrayis created usingmaketo holdninteger elements.
Step 4: Input the Array Elements
- The program uses a forloop to prompt the user to enter each element of the array, storing the input in the corresponding index ofarray.
Step 5: Sort the Array Using Bubble Sort
- The program implements the Bubble Sort algorithm by using nested forloops. The outer loop runs from0ton-1to ensure that all elements are sorted. The inner loop compares adjacent elements and swaps them if they are in the wrong order. The loop continues until no more swaps are needed.
Step 6: Display the Sorted Array
- The sorted array is displayed using fmt.Println.
Output Example
Example 1:
Enter the number of elements in the array: 5
Enter the elements of the array:
5
3
8
4
2
Sorted array: [2 3 4 5 8]
Example 2:
Enter the number of elements in the array: 4
Enter the elements of the array:
20
10
40
30
Sorted array: [10 20 30 40]
Conclusion
This Go program demonstrates how to sort an array using the Bubble Sort algorithm. It covers basic programming concepts such as arrays, loops, and conditional statements. This example is useful for beginners learning Go programming and understanding how to implement and optimize sorting algorithms.