Skip to content

Instantly share code, notes, and snippets.

@echohes
Last active February 28, 2022 14:46
Show Gist options
  • Save echohes/4a5518f8a67f569de55f3aa562826ac8 to your computer and use it in GitHub Desktop.
Save echohes/4a5518f8a67f569de55f3aa562826ac8 to your computer and use it in GitHub Desktop.
Merge 2 sort array
package main
import "fmt"
func main() {
a := []int{1, 2, 4, 5}
b := []int{2, 5, 6, 7, 8, 9}
c := []int{}
bs := 0
i := 0
m1 := &a
m2 := &b
if len(a) > len(b) {
m1 = &b
m2 = &a
}
for _, v := range *m1 {
for kk, vv := range *m2 {
if bs == 0 || kk >= bs {
if v < vv {
c = append(c, v)
i = i + 1
break
} else {
c = append(c, vv)
bs = bs + 1
i = i + 1
}
}
}
}
for k, v := range b {
if k >= bs {
c = append(c, v)
}
}
for k, v := range c {
fmt.Println(k, v)
}
}
a={1,1,3,5}
b={2,5,6,7,8,9}
c={}
i=1
as=0
bs=0
for k,v in pairs(a) do
if k>as or as==0 then
for kk,vv in pairs(b) do
if kk>bs or bs==0 then
if v<=vv then
c[i]=v
i=i+1
as=k
break
else
c[i]=vv
i=i+1
bs=kk
end
end
end
end
end
for k,v in pairs(b) do
if k>bs then
c[i]=v
i=i+1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment