go语言实现两个数组合并(merge)(二)

package main

import (
 
  "fmt"
)

func Merge() {   
   a := []int{3,6,9, 4,5,45}
   z := merge2(a, 0, 2, 5, 6)
   fmt.Printf("%#v\n", z)
}

//x数组,下标p,q,r 元素个数m。x[p]~x[q]及x[q+1]~x[r]已按递增排序
func merge2(x []int, p, q, r, m int) []int {
   z := make([]int, m)
   k := 0
   a := q
   b := q + 1
   for p
      if x[p]
         z[k] = x[p]
         p++
      } else {
         z[k] = x[b]
         b++
      }
      k++
   }
   for p != a+1 {
      z[k] = x[p]
      k++
      p++
   }
   
   for b != r+1 {
      z[k] = x[b]
      k++
      b++
   }
   return z
}

你可能感兴趣的:(go语言实现两个数组合并(merge)(二))