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 }
相關文章
相關標籤/搜索