diff --git a/MergeSort.go b/MergeSort.go new file mode 100644 index 0000000..6566ace --- /dev/null +++ b/MergeSort.go @@ -0,0 +1,149 @@ +package main + +import "fmt" +import "math/rand" +import "time" + +type Interface interface { + Len() int + Less(x, y int) bool + Equal(x, y int) bool + Swap(x, y int) + Get(x int) interface{} + Set(x int, src Interface, y int) +} + +type IntArr []int +func (IA IntArr) Len() int { return len(IA) } +func (IA IntArr) Less(x, y int) bool { return IA[x] < IA[y] } +func (IA IntArr) Equal(x, y int) bool { return IA[x] == IA[y] } +func (IA IntArr) Swap(x int, y int) { IA[x], IA[y] = IA[y], IA[x] } +func (IA IntArr) Get(x int) interface{} { return IA[x] } +func (IA IntArr) Set(x int, src Interface, y int) { IA[x] = src.Get(y).(int) } + +type StringArr []string +func (SA StringArr) Len() int { return len(SA) } +func (SA StringArr) Less(x, y int) bool { return SA[x] < SA[y] } +func (SA StringArr) Equal(x, y int) bool { return SA[x] == SA[y] } +func (SA StringArr) Swap(x int, y int) { SA[x], SA[y] = SA[y], SA[x] } +func (SA StringArr) Get(x int) interface{} { return SA[x] } +func (SA StringArr) Set(x int, src Interface, y int) { SA[x] = src.Get(y).(string) } + +type Float32Arr []float32 +func (F32A Float32Arr) Len() int { return len(F32A) } +func (F32A Float32Arr) Less(x, y int) bool { return F32A[x] < F32A[y] } +func (F32A Float32Arr) Equal(x, y int) bool { return F32A[x] == F32A[y] } +func (F32A Float32Arr) Swap(x int, y int) { F32A[x], F32A[y] = F32A[y], F32A[x] } +func (F32A Float32Arr) Get(x int) interface{} { return F32A[x] } +func (F32A Float32Arr) Set(x int, src Interface, y int) { F32A[x] = src.Get(y).(float32) } + +type Float64Arr []float64 +func (F64A Float64Arr) Len() int { return len(F64A) } +func (F64A Float64Arr) Less(x, y int) bool { return F64A[x] < F64A[y] } +func (F64A Float64Arr) Equal(x, y int) bool { return F64A[x] == F64A[y] } +func (F64A Float64Arr) Swap(x int, y int) { F64A[x], F64A[y] = F64A[y], F64A[x] } +func (F64A Float64Arr) Get(x int) interface{} { return F64A[x] } +func (F64A Float64Arr) Set(x int, src Interface, y int) { F64A[x] = src.Get(y).(float64) } + +func min(x, y int) int { + if x < y { + return x + } + return y +} + +func MergeSort(data, temp Interface) { + for n:=2; (n/2)