1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
func mergeSort(a []*models.Comparable) { l := len(a) mSort(a, 0, l-1) }
func mSort(a []*models.Comparable, lo, hi int) { if hi <= lo { return } mid := lo + (hi-lo)/2 mSort(a, lo, mid) mSort(a, mid+1, hi) merge(a, lo, mid, hi) }
func merge(a []*models.Comparable, lo, mid, hi int) { i, j := lo, mid+1 d := make([]*models.Comparable, 0) for _, k := range a { d = append(d, k) } for k := lo; k <= hi; k++ { if i > mid { a[k] = d[j] j++ } else if j > hi { a[k] = d[i] i++
} else if less(d[j], d[i]) { a[k] = d[j] j++ } else { a[k] = d[i] i++ } } }
|