-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingutils.go
More file actions
55 lines (45 loc) · 1.14 KB
/
sortingutils.go
File metadata and controls
55 lines (45 loc) · 1.14 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
44
45
46
47
48
49
50
51
52
53
54
55
package sorting
import (
"fmt"
"testing"
"io/ioutil"
"strconv"
"strings"
)
var FILENAME_TMPL string = "arraysize-10e%v-test%v.dat"
func readFile(fname string) (nums []int32, err error) {
b, err := ioutil.ReadFile(fname)
if err != nil { return nil, err }
lines := strings.Split(string(b), "\n")
nums = make([]int32, 0, len(lines))
for _, l := range lines {
if len(l) == 0 { continue }
n, err := strconv.Atoi(l)
if err != nil { return nil, err }
nums = append(nums, int32(n))
}
return nums, nil
}
type algo func([]int32)
func benchmark_algorithm(sorting_algo algo, complexity int, b *testing.B) {
b.StopTimer()
var integers []int32
for j := 0; j < b.N; j++ {
for i:=1; i<4; i++ {
filename := fmt.Sprintf(FILENAME_TMPL, complexity, i)
integers, _ = readFile(filename)
b.StartTimer()
sorting_algo(integers)
b.StopTimer()
}
}
}
func isSorted(t []int32) bool {
i := 1
for ; i<len(t); i++ {
if t[i] < t[i-1] {
return false
}
}
return true
}