selection sort Algorithm

Selection sort is a simple comparison-based sorting algorithm that is easy to understand and implement. The key idea behind the algorithm is to divide the input list into two parts – a sorted part and an unsorted part. Initially, the sorted part is empty, and the unsorted part contains all the elements. The algorithm repeatedly selects the smallest or largest (depending on the desired order) element from the unsorted part and moves it to the end of the sorted part. This process continues until the unsorted part becomes empty, and the sorted part contains all the elements in the desired order. In each iteration of the selection sort algorithm, it searches for the minimum or maximum element in the unsorted part of the list and swaps it with the first unsorted element. The time complexity of the selection sort algorithm is O(n^2) for the best, worst, and average cases, making it inefficient for large datasets. However, it performs well for small datasets or lists that are already partially sorted. One of the advantages of the selection sort algorithm is that it performs the minimum number of swaps, making it useful in situations where the cost of swapping elements is high. Despite its simplicity, selection sort is generally not used in practice because other sorting algorithms, such as quicksort and merge sort, have better overall performance.
package main

/*
 * Selection sort - http://en.wikipedia.org/wiki/Selection_sort
 */

func SelectionSort(arr []int) {
    var min int = 0
    var tmp int = 0

    for i := 0; i < len(arr); i++ {
        min = i
        for j := i + 1; j < len(arr); j++ {
            if arr[j] < arr[min] {
                min = j
            }
        }

        tmp = arr[i]
        arr[i] = arr[min]
        arr[min] = tmp
    }
}

LANGUAGE:

DARK MODE: