Welcome To Golang By Example

Selection Sort in Go(Golang)

Introduction

In selection sort, we maintain two parts

  1. Sorted Part
  2. Unsorted Part

Time Complexity

Space Complexity

Implementation:

package main

import "fmt"

func main() {
    sample := []int{3, 4, 5, 2, 1}
    selectionSort(sample)
    sample = []int{3, 4, 5, 2, 1, 7, 8, -1, -3}
    selectionSort(sample)
}

func selectionSort(arr []int) {
    len := len(arr)
    for i := 0; i < len-1; i++ {
        minIndex := i
        for j := i + 1; j < len; j++ {
            if arr[j] < arr[minIndex] {
                arr[j], arr[minIndex] = arr[minIndex], arr[j]
            }
        }
    }
    fmt.Println("\nAfter SelectionSort")
    for _, val := range arr {
        fmt.Println(val)
    }
}

Output:

After SelectionSort
1
2
3
4
5

After SelectionSort
-3
-1
1
2
3
4
5
7
8