Skip to content

Instantly share code, notes, and snippets.

@RichardKnop
Last active July 12, 2017 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RichardKnop/cc712fb1e5f2f91440816dc5d9f1b45a to your computer and use it in GitHub Desktop.
Save RichardKnop/cc712fb1e5f2f91440816dc5d9f1b45a to your computer and use it in GitHub Desktop.
Selection Sort
package main
import (
"fmt"
)
func main() {
items := []int{4, 202, 3, 9, 6, 5, 1, 43, 506, 2, 0, 8, 7, 100, 25, 4, 5, 97, 1000, 27}
selectionSort(items)
fmt.Println(items)
}
func selectionSort(items []int) {
var n = len(items)
for i := 0; i < n; i++ {
var minIdx = i
for j := i; j < n; j++ {
if items[j] < items[minIdx] {
minIdx = j
}
}
items[i], items[minIdx] = items[minIdx], items[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment