This is a problem from Robert Sedgwick's Algorithms book:
Write a version of bottom-up mergesort that takes advantage of order in the array by proceeding as follows each time it needs to find two arrays to merge: find a sorted subarray (by incrementing a pointer until finding an entry that is smaller than its predecessor in the array), then find the next, then merge them.
The code needs to be reviewed for:
- Correct use of Go
- Any improvements to the code
// NaturalMergeSort.go
package main
import (
"fmt"
)
type sorting struct {
arr []int
aux []int
}
func newSorting(arr []int) *sorting {
s := new(sorting)
s.arr = arr
s.aux = make([]int, len(s.arr))
return s
}
func (s *sorting) naturalSort() {
if len(s.arr) <= 1 {
return
}
start := 0
stop1 := s.getNextStop(start)
var stop2 int
for stop1 < len(s.arr)-1 {
stop2 = s.getNextStop(stop1 + 1)
// Now merge arrays
//fmt.Println(start, stop1, stop2)
s.merge(start, stop1, stop2)
stop1 = stop2
}
}
func (s *sorting) merge(start int, stop1 int, stop2 int) {
// copy first part into aux
for k := start; k <= stop1; k++ {
s.aux[k] = s.arr[k]
}
i := start
j := stop1 + 1
for k := start; k <= stop2; k++ {
if i > stop1 {
// copying is done
break
} else if j > stop2 {
s.arr[k] = s.aux[i]
i++
} else if s.aux[i] > s.arr[j] {
s.arr[k] = s.arr[j]
j++
} else {
s.arr[k] = s.aux[i]
i++
}
}
}
func (s *sorting) getNextStop(i int) int {
for i < len(s.arr)-1 && s.arr[i] < s.arr[i+1] {
i = i + 1
}
return i
}
func main() {
s := newSorting([]int{5, 3, 4, 10, 1, 6, 8, 2})
s.naturalSort()
fmt.Println(s.arr)
}