-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
43 lines (38 loc) · 1.26 KB
/
main.go
File metadata and controls
43 lines (38 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// This program runs through a few sort algorithms
// to demonstrate how different sorting algorithms works.
// This was written to get comfortable with Go, and to demonstrate to my son
// the differences between different sorting algorithms.
package main
import "fmt"
import "github.com/cxcheng/sortgo/sortlib"
func printVals(label string, vals []sortlib.Val) {
fmt.Print(label, ":")
for _, val := range(vals) {
fmt.Print(" ", val.String())
}
fmt.Println()
}
func main() {
// generate array of random integers and loop through each sort function
vals := sortlib.RandomIVals(20, 999)
for _, f := range(sortlib.SortFuncs) {
// perform sort and print results
ctx := sortlib.NewCtx()
vals2 := make([]sortlib.Val, len(vals), len(vals))
copy(vals2, vals)
ctx.Sort(f, &vals2)
ctx.Print()
}
// test the strings
svals := sortlib.SValsFromFile("text/2planets.txt")
for _, f := range(sortlib.SortFuncs) {
// perform sort and print results
ctx := sortlib.NewCtx()
svals2 := make([]sortlib.Val, len(svals), len(svals))
copy(svals2, svals)
printVals("Before", svals2)
ctx.Sort(f, &svals2)
ctx.Print()
printVals("After", svals2)
}
}