Skip to content

Instantly share code, notes, and snippets.

@hankliu5
Created July 13, 2018 03:17
Show Gist options
  • Save hankliu5/edca7e6dafc3ec753fab811f7ca0001d to your computer and use it in GitHub Desktop.
Save hankliu5/edca7e6dafc3ec753fab811f7ca0001d to your computer and use it in GitHub Desktop.
func Merge(ldata []float64, rdata []float64) (result []float64) {
result = make([]float64, len(ldata)+len(rdata))
lidx, ridx := 0, 0
for i := 0; i < cap(result); i++ {
switch {
case lidx >= len(ldata):
result[i] = rdata[ridx]
ridx++
case ridx >= len(rdata):
result[i] = ldata[lidx]
lidx++
case ldata[lidx] < rdata[ridx]:
result[i] = ldata[lidx]
lidx++
default:
result[i] = rdata[ridx]
ridx++
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment