-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (48 loc) · 946 Bytes
/
main.go
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
56
57
58
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
// TestData struct for json test data
type TestData struct {
Data []int `json:"data"`
}
type sortFunction func(arr []int) []int
func countElapse(sf sortFunction, arr []int) {
start := time.Now()
fmt.Println(sf(arr))
elapsed := time.Since(start)
log.Printf("Insertion Sort took %s", elapsed)
}
func openTestData() []int {
jsonFile, err := os.Open("./testdata.json")
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
var testData TestData
json.Unmarshal([]byte(byteValue), &testData)
return testData.Data
}
func insertionSort(arr []int) []int {
i := 1
for i < len(arr) {
currentValue := arr[i]
j := i - 1
for j >= 0 && currentValue < arr[j] {
arr[j+1] = arr[j]
j--
}
arr[j+1] = currentValue
i++
}
return arr
}
func main() {
testArray := openTestData()
countElapse(insertionSort, testArray)
}