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))
}