Golang Program to Implement Selection Sort


March 31, 2022, Learn eTutorial
921

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

In this Go program, we need to sort a set of elements from an array by using Selection sort. Selection sort is a very common and simple sorting algorithm.  It works under the principle of an in-place comparison-based algorithm. 

In this technique, it will sort the elements by repeatedly finding the minimum element and swapping with the leftmost element, and that element becomes a part of the sorted array. After that, the array is divided into two parts, the sorted part as well as the unsorted part. We can see the sorted part at the left end and the unsorted part at the right end which can be again sorted using the algorithm. This sorting process continues moving unsorted array boundary by one element to the right. The average and worst-case complexities of selection sort are of Ο(n2), where n is the number of items. So it is not suitable for large data sets.

How to Implement Selection Sort

Here we are explaining how to perform Selection 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 and it 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 and perform Selection sort by calling function Selection_Sort(array, num)). This function will try to find an index of the minimum element by iterating over the array. If the minimum number is found, swap the lowest element with its previous element. Repeat this process until the list is sorted. Finally, the sorted array is returned to the main function which displays the result sorted array by using for loop.

Given below are the steps which are used in the Go program to implement Selection Sort. 

ALGORITHM:

STEP 1: Import the packages 'fmt'.
STEP 2: Open the main() to start the program, GO program execution starts with the main()
STEP 3: Declare the variable n.
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: Perform selection sort by calling function Selection_Sort(array, num))
STEP 8: Use for loop to display sorted array.
STEP 9: Exit

Steps To Implement Selection_Sort(array[] int, size int)

STEP 1: Find the index of the minimum element min by iterating over the array.
STEP 2: If the minimum number is found, then swap with its previous element.
STEP 3: Increment min to point to next element
STEP 4: Repeat until the array is sorted
STEP 5: Return the sorted array
 


 

Golang Source Code

                                          package main
import "fmt"
func Selection_Sort(A[] int, n int) []int {
   var min int
   var temp int
   for i := 0; i < n- 1; i++ {
      min= i
      // Find index of minimum element
      for j := i + 1; j < n; j++ {
         if A[j] < A[min] {
            min= j
         }
      }
      temp = A[i]
      A[i] = A[min]
      A[min] = temp
   }
   return A
}
func main() {
   fmt.Println("Enter the size of the array:")
 var n int
 fmt.Scan(&n)
 A := make([]int, n, 100)
fmt.Println("Enter elements of the array : ")


 for i := 0; i < n; i++ {
  fmt.Scan(&A[i])
 }
fmt.Println("Sorted array:")
   fmt.Println(Selection_Sort(A, n))
}

                                      

OUTPUT

Enter the size of the array:
5
Enter elements of the array : 
69
32
52
74
12
Sorted array:
[12 32 52 69 74]