-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path88.MergeSortedArray.go
92 lines (81 loc) · 1.39 KB
/
88.MergeSortedArray.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
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"sort"
)
func merge(nums1 []int, m int, nums2 []int, n int) {
// first add elements in nums2 to the end of nums1, // then sort the entire nums1
j:=0
for i:=m;i<len(nums1);i++{
nums1[i]=nums2[j]
j++
}
// some sorting algorithm - QuickSort built-in
sort.Ints(nums1)
/*
//An inefficient implementation :
if m==0{
nums1 = append(nums1[:0],nums2...)
return
}
cnt:=0
for j:=0;j<n;j++{
if nums2[j] < nums1[m+cnt-1] {
for i := 0; i < m+cnt; i++ {
if nums2[j] < nums1[i] {
//insert
for ii := m + n - 1; ; ii-- {
nums1[ii] = nums1[ii-1]
if ii == i+1 {
break
}
}
nums1[i] = nums2[j]
cnt++
break
}
}
}else{
//append after all non zero elements of num1
nums1[m+cnt]=nums2[j]
cnt++
}
}
*/
/*
// Fastest , better implementation
index1 := m - 1
index2 := n - 1
index3 := m + n - 1
for {
if index1 < 0 || index2 < 0 {
break
}
if nums1[index1] > nums2[index2] {
nums1[index3] = nums1[index1]
index1--
index3--
} else {
nums1[index3] = nums2[index2]
index2--
index3--
}
}
for ; index2 >= 0; {
nums1[index3] = nums2[index2]
index2--
index3--
}
*/
}
func main() {
ip1 := []int{4,0,0,0,0,0}
m := 1
ip2 := []int{1,2,3,5,6}
n := len(ip2)
merge(ip1,m,ip2,n)
fmt.Println("Final answer")
for i:=0;i<(len(ip1));i++{
fmt.Println(ip1[i])
}
}