-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.go
55 lines (44 loc) · 1.11 KB
/
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
package main
import (
"fmt"
"math"
"math/rand"
"time"
"github.com/donng/Play-with-Data-Structures/08-Heap-and-Priority-Queue/05-Heapify-and-Replace-in-Heap/maxheap"
"github.com/donng/Play-with-Data-Structures/utils"
)
func testHeap(testData []interface{}, isHeapify bool) time.Duration {
startTime := time.Now()
var maxHeap *maxheap.MaxHeap
if isHeapify {
maxHeap = maxheap.GetMaxHeapFromArr(testData)
} else {
maxHeap = maxheap.New()
for _, v := range testData {
maxHeap.Add(v)
}
}
arr := make([]interface{}, len(testData))
for i := 0; i < len(testData); i++ {
arr[i] = maxHeap.ExtractMax()
}
for i := 1; i < len(testData); i++ {
if utils.Compare(arr[i-1], arr[i]) < 0 {
panic("Error")
}
}
fmt.Println("test MaxHeap completed")
return time.Now().Sub(startTime)
}
func main() {
n := 1000000
var testData []interface{}
rand.Seed(time.Now().Unix())
for i := 0; i < n; i++ {
testData = append(testData, rand.Intn(math.MaxInt32))
}
time1 := testHeap(testData, false)
fmt.Println("Without heapify: ", time1)
time2 := testHeap(testData, true)
fmt.Println("With heapify: ", time2)
}