package kata func merge_sort(slice []int) []int { length := len(slice) if length < 2 { return slice } mid := (length) / 2 return merge(merge_sort(slice[:mid]), merge_sort(slice[mid:])) } func merge(left, right []int) []int { size, l, r := len(left)+len(right), 0, 0 slice := make([]int, size, size) for i := 0; i < size; i++ { if l > len(left)-1 && r <= len(right)-1 { slice[i] = right[r] r++ } else if r > len(right)-1 && l <= len(left)-1 { slice[i] = left[l] l++ } else if left[l] < right[r] { slice[i] = left[l] l++ } else { slice[i] = right[r] r++ } } return slice } func MergeSort(arr []int) []int { return merge_sort(arr) }
package mainimport "fmt"- package kata
- func merge_sort(slice []int) []int {
- length := len(slice)
- if length < 2 {
- return slice
- }
- mid := (length) / 2
- return merge(merge_sort(slice[:mid]), merge_sort(slice[mid:]))
- }
- func merge(left, right []int) []int {
- size, l, r := len(left)+len(right), 0, 0
- slice := make([]int, size, size)
- for i := 0; i < size; i++ {
- if l > len(left)-1 && r <= len(right)-1 {
- slice[i] = right[r]
- r++
- } else if r > len(right)-1 && l <= len(left)-1 {
- slice[i] = left[l]
- l++
- } else if left[l] < right[r] {
- slice[i] = left[l]
- l++
- } else {
- slice[i] = right[r]
- r++
- }
- }
- return slice
- }
func main() {arr := []int{3, 4, 1, 2, 5}fmt.Print(merge_sort(arr))- func MergeSort(arr []int) []int {
- return merge_sort(arr)
- }
// TODO: replace with your own tests (TDD). An example to get you started is included below. // Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/> // Gomega Matcher Library <http://onsi.github.io/gomega/> package kata_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "codewarrior/kata" ) var _ = Describe("Test Example", func() { It("Basic Tests", func() { Expect(MergeSort([]int{1, 3, 2})).To(Equal([]int{1, 2, 3})) Expect(MergeSort([]int{3, 4, 1, 2, 5})).To(Equal([]int{1, 2, 3, 4, 5})) }) }) var _ = Describe("Cooler tests", func() { It("some edge cases etc", func() { }) }) var _ = Describe("Random tests", func() { It("small", func() { }) It("middle", func() { }) It("big", func() { }) })
- // TODO: replace with your own tests (TDD). An example to get you started is included below.
- // Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/>
- // Gomega Matcher Library <http://onsi.github.io/gomega/>
- package kata_test
- import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "codewarrior/kata"
- )
- var _ = Describe("Test Example", func() {
- It("Basic Tests", func() {
- Expect(MergeSort([]int{1, 3, 2})).To(Equal([]int{1, 2, 3}))
- Expect(MergeSort([]int{3, 4, 1, 2, 5})).To(Equal([]int{1, 2, 3, 4, 5}))
- })
- })
- var _ = Describe("Cooler tests", func() {
- It("some edge cases etc", func() {
- })
- })
- var _ = Describe("Random tests", func() {
- It("small", func() {
- })
- It("middle", func() {
- })
- It("big", func() {
- })
- })