| 1 |
package sorts |
| 2 |
|
| 3 |
import ( |
| 4 |
"sort" |
| 5 |
) |
| 6 |
|
| 7 |
// Copyright 2009 The Go Authors. |
| 8 |
// Copyright 2014-5 Randall Farmer. |
| 9 |
// All rights reserved. |
| 10 |
|
| 11 |
// Use of this source code is governed by a BSD-style license that can be |
| 12 |
// found in the LICENSE file. |
| 13 |
|
| 14 |
// This copies code from Go's sort.go because we can't use something like |
| 15 |
// sort.SortRange(data, a, b) to sort a range of data. Wrapping incoming |
| 16 |
// data in another sort.Interface is possible, but kills speed. |
| 17 |
|
| 18 |
// Insertion sort |
| 19 |
func insertionSort(data sort.Interface, a, b int) { |
| 20 |
for i := a + 1; i < b; i++ { |
| 21 |
for j := i; j > a && data.Less(j, j-1); j-- { |
| 22 |
data.Swap(j, j-1) |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
// siftDown implements the heap property on data[lo, hi). |
| 28 |
// first is an offset into the array where the root of the heap lies. |
| 29 |
func siftDown(data sort.Interface, lo, hi, first int) { |
| 30 |
root := lo |
| 31 |
for { |
| 32 |
child := 2*root + 1 |
| 33 |
if child >= hi { |
| 34 |
break |
| 35 |
} |
| 36 |
if child+1 < hi && data.Less(first+child, first+child+1) { |
| 37 |
child++ |
| 38 |
} |
| 39 |
if !data.Less(first+root, first+child) { |
| 40 |
return |
| 41 |
} |
| 42 |
data.Swap(first+root, first+child) |
| 43 |
root = child |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func heapSort(data sort.Interface, a, b int) { |
| 48 |
first := a |
| 49 |
lo := 0 |
| 50 |
hi := b - a |
| 51 |
|
| 52 |
// Build heap with greatest element at top. |
| 53 |
for i := (hi - 1) / 2; i >= 0; i-- { |
| 54 |
siftDown(data, i, hi, first) |
| 55 |
} |
| 56 |
|
| 57 |
// Pop elements, largest first, into end of data. |
| 58 |
for i := hi - 1; i >= 0; i-- { |
| 59 |
data.Swap(first, first+i) |
| 60 |
siftDown(data, lo, i, first) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// medianOfThree moves the median of the three values data[m0], data[m1], data[m2] into data[m1]. |
| 65 |
func medianOfThree(data sort.Interface, m1, m0, m2 int) { |
| 66 |
// sort 3 elements |
| 67 |
if data.Less(m1, m0) { |
| 68 |
data.Swap(m1, m0) |
| 69 |
} |
| 70 |
// data[m0] <= data[m1] |
| 71 |
if data.Less(m2, m1) { |
| 72 |
data.Swap(m2, m1) |
| 73 |
// data[m0] <= data[m2] && data[m1] < data[m2] |
| 74 |
if data.Less(m1, m0) { |
| 75 |
data.Swap(m1, m0) |
| 76 |
} |
| 77 |
} |
| 78 |
// now data[m0] <= data[m1] <= data[m2] |
| 79 |
} |
| 80 |
|
| 81 |
func doPivot(data sort.Interface, lo, hi int) (midlo, midhi int) { |
| 82 |
m := lo + (hi-lo)/2 // Written like this to avoid integer overflow. |
| 83 |
if hi-lo > 40 { |
| 84 |
// Tukey's ``Ninther,'' median of three medians of three. |
| 85 |
s := (hi - lo) / 8 |
| 86 |
medianOfThree(data, lo, lo+s, lo+2*s) |
| 87 |
medianOfThree(data, m, m-s, m+s) |
| 88 |
medianOfThree(data, hi-1, hi-1-s, hi-1-2*s) |
| 89 |
} |
| 90 |
medianOfThree(data, lo, m, hi-1) |
| 91 |
|
| 92 |
// Invariants are: |
| 93 |
// data[lo] = pivot (set up by ChoosePivot) |
| 94 |
// data[lo < i < a] < pivot |
| 95 |
// data[a <= i < b] <= pivot |
| 96 |
// data[b <= i < c] unexamined |
| 97 |
// data[c <= i < hi-1] > pivot |
| 98 |
// data[hi-1] >= pivot |
| 99 |
pivot := lo |
| 100 |
a, c := lo+1, hi-1 |
| 101 |
|
| 102 |
for ; a != c && data.Less(a, pivot); a++ { |
| 103 |
} |
| 104 |
b := a |
| 105 |
for { |
| 106 |
for ; b != c && !data.Less(pivot, b); b++ { // data[b] <= pivot |
| 107 |
} |
| 108 |
for ; b != c && data.Less(pivot, c-1); c-- { // data[c-1] > pivot |
| 109 |
} |
| 110 |
if b == c { |
| 111 |
break |
| 112 |
} |
| 113 |
// data[b] > pivot; data[c-1] <= pivot |
| 114 |
data.Swap(b, c-1) |
| 115 |
b++ |
| 116 |
c-- |
| 117 |
} |
| 118 |
// If hi-c<3 then there are duplicates (by property of median of nine). |
| 119 |
// Let be a bit more conservative, and set border to 5. |
| 120 |
protect := hi-c < 5 |
| 121 |
if !protect && hi-c < (hi-lo)/4 { |
| 122 |
// Lets test some points for equality to pivot |
| 123 |
dups := 0 |
| 124 |
if !data.Less(pivot, hi-1) { // data[hi-1] = pivot |
| 125 |
data.Swap(c, hi-1) |
| 126 |
c++ |
| 127 |
dups++ |
| 128 |
} |
| 129 |
if !data.Less(b-1, pivot) { // data[b-1] = pivot |
| 130 |
b-- |
| 131 |
dups++ |
| 132 |
} |
| 133 |
// m-lo = (hi-lo)/2 > 6 |
| 134 |
// b-lo > (hi-lo)*3/4-1 > 8 |
| 135 |
// ==> m < b ==> data[m] <= pivot |
| 136 |
if !data.Less(m, pivot) { // data[m] = pivot |
| 137 |
data.Swap(m, b-1) |
| 138 |
b-- |
| 139 |
dups++ |
| 140 |
} |
| 141 |
// if at least 2 points are equal to pivot, assume skewed distribution |
| 142 |
protect = dups > 1 |
| 143 |
} |
| 144 |
if protect { |
| 145 |
// Protect against a lot of duplicates |
| 146 |
// Add invariant: |
| 147 |
// data[a <= i < b] unexamined |
| 148 |
// data[b <= i < c] = pivot |
| 149 |
for { |
| 150 |
for ; a != b && !data.Less(b-1, pivot); b-- { // data[b] == pivot |
| 151 |
} |
| 152 |
for ; a != b && data.Less(a, pivot); a++ { // data[a] < pivot |
| 153 |
} |
| 154 |
if a == b { |
| 155 |
break |
| 156 |
} |
| 157 |
// data[a] == pivot; data[b-1] < pivot |
| 158 |
data.Swap(a, b-1) |
| 159 |
a++ |
| 160 |
b-- |
| 161 |
} |
| 162 |
} |
| 163 |
// Swap pivot into middle |
| 164 |
data.Swap(pivot, b-1) |
| 165 |
return b - 1, c |
| 166 |
} |
| 167 |
|
| 168 |
func quickSort(data sort.Interface, a, b, maxDepth int) { |
| 169 |
for b-a > 12 { |
| 170 |
if maxDepth == 0 { |
| 171 |
heapSort(data, a, b) |
| 172 |
return |
| 173 |
} |
| 174 |
maxDepth-- |
| 175 |
mlo, mhi := doPivot(data, a, b) |
| 176 |
// Avoiding recursion on the larger subproblem guarantees |
| 177 |
// a stack depth of at most lg(b-a). |
| 178 |
if mlo-a < b-mhi { |
| 179 |
quickSort(data, a, mlo, maxDepth) |
| 180 |
a = mhi // i.e., quickSort(data, mhi, b) |
| 181 |
} else { |
| 182 |
quickSort(data, mhi, b, maxDepth) |
| 183 |
b = mlo // i.e., quickSort(data, a, mlo) |
| 184 |
} |
| 185 |
} |
| 186 |
if b-a > 1 { |
| 187 |
// Do ShellSort pass with gap 6 |
| 188 |
// It could be written in this simplified form cause b-a <= 12 |
| 189 |
for i := a + 6; i < b; i++ { |
| 190 |
if data.Less(i, i-6) { |
| 191 |
data.Swap(i, i-6) |
| 192 |
} |
| 193 |
} |
| 194 |
insertionSort(data, a, b) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// qSort quicksorts data immediately. |
| 199 |
// It performs O(n*log(n)) comparisons and swaps. The sort is not stable. |
| 200 |
func qSort(data sort.Interface, a, b int) { |
| 201 |
// Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached. |
| 202 |
n := b - a |
| 203 |
maxDepth := 0 |
| 204 |
for i := n; i > 0; i >>= 1 { |
| 205 |
maxDepth++ |
| 206 |
} |
| 207 |
maxDepth *= 2 |
| 208 |
quickSort(data, a, b, maxDepth) |
| 209 |
} |
| 210 |
|
| 211 |
// Quicksort performs a parallel quicksort on data. |
| 212 |
func Quicksort(data sort.Interface) { |
| 213 |
a, b := 0, data.Len() |
| 214 |
n := b - a |
| 215 |
maxDepth := 0 |
| 216 |
for i := n; i > 0; i >>= 1 { |
| 217 |
maxDepth++ |
| 218 |
} |
| 219 |
maxDepth *= 2 |
| 220 |
parallelSort(data, quickSortWorker, task{-maxDepth - 1, a, b}) |
| 221 |
} |
| 222 |
|
| 223 |
// qSortPar starts a parallel quicksort. |
| 224 |
func qSortPar(data sort.Interface, t task, sortRange func(task)) { |
| 225 |
a, b := t.pos, t.end |
| 226 |
n := b - a |
| 227 |
maxDepth := 0 |
| 228 |
for i := n; i > 0; i >>= 1 { |
| 229 |
maxDepth++ |
| 230 |
} |
| 231 |
maxDepth *= 2 |
| 232 |
quickSortWorker(data, task{-maxDepth - 1, a, b}, sortRange) |
| 233 |
} |
| 234 |
|
| 235 |
// quickSortWorker is a parallel analogue of quickSort: it performs a pivot |
| 236 |
// and might asynchronously sort one of the halves if it's large enough. |
| 237 |
func quickSortWorker(data sort.Interface, t task, sortRange func(task)) { |
| 238 |
maxDepth, a, b := 1-t.offs, t.pos, t.end |
| 239 |
for b-a > minOffload { |
| 240 |
if maxDepth == 0 { |
| 241 |
heapSort(data, a, b) |
| 242 |
return |
| 243 |
} |
| 244 |
maxDepth-- |
| 245 |
mlo, mhi := doPivot(data, a, b) |
| 246 |
// Avoiding recursion on the larger subproblem guarantees |
| 247 |
// a stack depth of at most lg(b-a). |
| 248 |
if mlo-a < b-mhi { |
| 249 |
sortRange(task{-maxDepth - 1, a, mlo}) |
| 250 |
a = mhi // i.e., quickSortWorker(data, mhi, b) |
| 251 |
} else { |
| 252 |
sortRange(task{-maxDepth - 1, mhi, b}) |
| 253 |
b = mlo // i.e., quickSortWorker(data, a, mlo) |
| 254 |
} |
| 255 |
} |
| 256 |
if b-a > 7 { |
| 257 |
quickSort(data, a, b, maxDepth) |
| 258 |
} else if b-a > 1 { |
| 259 |
insertionSort(data, a, b) |
| 260 |
} |
| 261 |
} |