Created
March 24, 2014 16:15
-
-
Save Fosome/9743505 to your computer and use it in GitHub Desktop.
bubble sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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