-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel03.go
81 lines (73 loc) · 1.47 KB
/
level03.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"reflect"
"runtime"
"time"
)
func level03() {
const Max = 50000
var n, r [Max]int
sorts := []func([]int){selectionSort, bubbleSort, insertionSort}
for _, s := range sorts {
name := runtime.FuncForPC(reflect.ValueOf(s).Pointer()).Name()
logger.Printf("* %20s - Sorted Ascending", name)
for i := 0; i < Max; i++ {
n[i] = i + 1
}
sortTime(s, n[:])
logger.Printf("* %20s - Sorted Descending", name)
for i := 0; i < Max; i++ {
r[i] = Max - i
}
sortTime(s, r[:])
}
}
func sortTime(f func([]int), n []int) {
// logger.Printf("(%d) %v BEFORE", len(n), n)
start := time.Now()
f(n)
stop := time.Now()
// logger.Printf("(%d) %v AFTER", len(n), n)
logger.Println("Time to sort: ", stop.Sub(start))
}
func selectionSort(n []int) {
for i := 0; i < len(n); i++ {
min_j, min := i, n[i]
for j := i; j < len(n); j++ {
if n[j] < min {
min_j, min = j, n[j]
}
}
if min_j != i {
n[i], n[min_j] = n[min_j], n[i]
}
}
}
func bubbleSort(n []int) {
swapped := true
for swapped {
swapped = false
for i := 0; i < len(n)-1; i++ {
if n[i] > n[i+1] {
swapped = true
n[i], n[i+1] = n[i+1], n[i]
}
}
}
}
func insertionSort(n []int) {
for i := 0; i < len(n)-1; i++ {
if n[i+1] < n[i] {
value := n[i+1]
var j int
for j = i; j >= 0; j-- {
if n[j] > value {
n[j+1] = n[j]
// logger.Printf("i=%d, j=%d, n[i]=%d, n[j]=%d, n=%v\n",
// i, j, n[i], n[j], n)
}
}
n[j+1] = value
}
}
}