forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.go
47 lines (43 loc) · 1.07 KB
/
merge_sort.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
package main
import "fmt"
// MergeSort divides the slice into two parts, sorts both the part of slice and then merge it
// Time complexity : O(size(log(size)))
// Space complexity : O(size)
func MergeSort(slice []int) []int {
if len(slice) < 2 {
return slice
}
mid := (len(slice)) / 2
return merge(MergeSort(slice[:mid]), MergeSort(slice[mid:]))
}
// merge merges two sorted slice
// Time complexity : O(size)
// Space complexity : O(size)
func merge(left []int, right []int) []int {
i := 0
j := 0
size := len(left) + len(right)
slice := make([]int, size, size) // make built-in function allocates and initializes an object of type slice
for k := 0; k < size; k++ {
if i > len(left)-1 && j < len(right) {
slice[k] = right[j]
j++
} else if j > len(right)-1 && i < len(left) {
slice[k] = left[i]
i++
} else if left[i] < right[j] {
slice[k] = left[i]
i++
} else {
slice[k] = right[j]
j++
}
}
return slice
}
func main() {
slice := []int{8, 2, 4, 9, 1, 699, 0, 9999}
fmt.Println("Sorted data is:")
slice = MergeSort(slice)
fmt.Println(slice)
}