Skip to content

Instantly share code, notes, and snippets.

@Fosome
Created March 24, 2014 16:15
Show Gist options
  • Save Fosome/9743505 to your computer and use it in GitHub Desktop.
Save Fosome/9743505 to your computer and use it in GitHub Desktop.
bubble sort
// sort.go
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
// Read arguments into ary
args := os.Args[1:]
ary := make([]int64, len(args))
for i, arg := range args {
ary[i], _ = strconv.ParseInt(arg, 0, 64)
}
// Bubble sort
swapped := true
for swapped {
swapped = false
for i := 1; i < len(ary); i++ {
if ary[i-1] > ary[i] {
ary[i-1], ary[i] = ary[i], ary[i-1]
swapped = true
}
}
}
// Show results
fmt.Println("Sorted:", ary)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment