Having trouble with merge sort in Golang

Hi all. I was able to do merge sort in Python and then decided to try to do it with Go. The Python code is almost identical to the Golang version below. I can’t seem to figure out where it’s going wrong though. Any help is appreciated.

package main

import (
	"fmt"
)

func merge(left []uint8, right []uint8) []uint8 {
	var combined []uint8
	leftIndex, rightIndex := 0, 0
	for leftIndex < len(left) && rightIndex < len(right) {
		if left[leftIndex] < right[rightIndex] {
			combined = append(combined, left[leftIndex])
			leftIndex++
		} else {
			combined = append(combined, right[rightIndex])
			rightIndex++
		}
	}
	combined = append(left[leftIndex:len(left)], right[rightIndex:len(right)]...)
	return combined
}

func mergeSort(current []uint8) []uint8 {
	if len(current) < 2 {
		return current
	}
	m := int(len(current) / 2)
	return merge(mergeSort(current[0:m]), mergeSort(current[m:len(current)]))
}

func main() {
	unsorted := []uint8{8, 6, 3, 0, 4, 5, 3, 9, 2}
	fmt.Printf("%v\n", mergeSort(unsorted))
}


Check the last append of combined in merge function

Ah I see that I’m not actually appending it back to combined… hmmm thanks!

Thanks yah01… I used these lines instead:

temp := append(left[leftIndex:len(left)], right[rightIndex:len(right)]...)
combined = append(combined, temp...)
return combined

yes, it’s correct. Or just append left and right to combined separately, I may choose this way