Golang Program to Implement Bubble Sort


February 24, 2024, Learn eTutorial
1255

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

Here we are explaining how to write a GO program to sort a list of elements from any data structure. There are different types of sorting algorithms. In this case, we use a Bubble Sort for sorting array elements.

Bubble Sort is one of the most simple and common sorting methods. Suppose we have an unordered list and want to sort these elements, then the Bubble algorithm compares each pair of adjacent elements and the elements are swapped if they are not in order. So we can say it is a comparison-based algorithm. It will check if the adjacent elements are in the right order (increasing) or not by multiple passes. When we implement a bubble sort algorithm, we can know how many times to do a swap and this is equal to the length of the list minus 1. Note that, the average and worst-case complexity of bubble sort is Ο(n2) where n is the number of items. So, it is not suitable for large data sets.

How to Implement Bubble Sort

Here we are explaining how to perform Bubble Sort in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package which helps to write standard output. In order to use these functions, we need to import the “fmt” package.

Here variable A holds the array elements. Another variable n is used as the size of the array. Use for loop to read array elements. Compare each element with its adjacent element by using two nested 'for loop' and swap the element if it is less than the previous element. Finally, we get results as a sorted array. Use for loop to print the sorted array.

ALGORITHM:

STEP 1: Import the package fmt
STEP 2: Open the main() to start the program, GO program execution starts with the main()
STEP 3: Declare the variables n and temp.
STEP 4: Read the size of the array as n
STEP 5: Define the array A[]
STEP 6: Read the A[] array elements by using a 'for loop'.
STEP 7: Open the nested 'for loop' to compare each element with its adjacent element
STEP 8: Using an if condition check, the comparing element is greater than the compared element otherwise go to step 10
STEP 9: If so, swap the elements using the temporary variable temp.
STEP 10: Print the sorted Array using 'for loop' and fmt.Println
STEP 11: Exit

Golang Source Code

                                          package main
import "fmt"
func main() {
 fmt.Println("Enter the size of the array")
 var n int
 var temp int
 fmt.Scan(&n)
 A := make([]int, n, 100)
fmt.Println("Enter elements : ")

 for i := 0; i < n; i++ {
  fmt.Scan(&A[i])
 }


for i := 0; i < n; i++ {

for j := 0; j <(n-i-1); j++ {
   if A[j] > A[j + 1]{
           temp = A[j];
           A[j] = A[j + 1];
           A[j + 1] = temp;
         }
      }
  }

fmt.Println("Sorted array : ")

 for i := 0; i < n; i++ {
  fmt. Println (A[i])
 }
}
                                      

OUTPUT

Enter the size of the array
4
Enter elements : 
8
3
5
1
Sorted array : 
1
3
5
8