golang sorting code example
Example 1: Sort an array in Go
s := []int{4, 2, 3, 1}
sort.Ints(s)
fmt.Println(s)
Example 2: bubble sort in golang
package main
import (
"fmt"
"os"
"strconv"
)
func bubblesort() {
unsorted := os.Args[1:]
var data [5]int
for i, n := range unsorted {
value, err := strconv.Atoi(n)
data[i] = value
if err != nil {
data[i] = 0
}
}
for i := 0; i < len(data)-1; i++ {
for j := 0; j < len(data)-i-1; j++ {
if data[j] > data[j+1] {
data[j], data[j+1] = data[j+1], data[j]
}
}
}
fmt.Println(data)
}