github.com/chenjiandongx/go-queue@v0.0.0-20191023082232-e2a36f382f84/std_sort.go (about) 1 package collections 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 //go:generate go run genzfunc.go 8 9 // Package sort provides primitives for sorting slices and user-defined 10 // collections. 11 12 // Insertion sort 13 func insertionSort(data []int, a, b int) { 14 for i := a + 1; i < b; i++ { 15 for j := i; j > a && data[j] < data[j-1]; j-- { 16 data[j], data[j-1] = data[j-1], data[j] 17 } 18 } 19 } 20 21 // siftDown implements the heap property on data[lo, hi). 22 // first is an offset into the array where the root of the heap lies. 23 func siftDown(data []int, lo, hi, first int) { 24 root := lo 25 for { 26 child := 2*root + 1 27 if child >= hi { 28 break 29 } 30 if child+1 < hi && data[first+child] < data[first+child+1] { 31 child++ 32 } 33 if !(data[first+root] < data[first+child]) { 34 return 35 } 36 data[first+root], data[first+child] = data[first+child], data[first+root] 37 root = child 38 } 39 } 40 41 func heapSort(data []int, a, b int) { 42 first := a 43 lo := 0 44 hi := b - a 45 46 // Build heap with greatest element at top. 47 for i := (hi - 1) / 2; i >= 0; i-- { 48 siftDown(data, i, hi, first) 49 } 50 51 // Pop elements, largest first, into end of data. 52 for i := hi - 1; i >= 0; i-- { 53 data[first], data[first+i] = data[first+i], data[first] 54 siftDown(data, lo, i, first) 55 } 56 } 57 58 // Quicksort, loosely following Bentley and McIlroy, 59 // ``Engineering a Sort Function,'' SP&E November 1993. 60 61 // medianOfThree moves the median of the three values data[m0], data[m1], data[m2] into data[m1]. 62 func medianOfThree(data []int, m1, m0, m2 int) { 63 // sort 3 elements 64 if data[m1] < data[m0] { 65 data[m1], data[m0] = data[m0], data[m1] 66 } 67 // data[m0] <= data[m1] 68 if data[m2] < data[m1] { 69 data[m2], data[m1] = data[m1], data[m2] 70 if data[m1] < data[m0] { 71 data[m1], data[m0] = data[m0], data[m1] 72 } 73 } 74 // now data[m0] <= data[m1] <= data[m2] 75 } 76 77 func doPivot(data []int, lo, hi int) (midlo, midhi int) { 78 m := int(uint(lo+hi) >> 1) // Written like this to avoid integer overflow. 79 if hi-lo > 40 { 80 // Tukey's ``Ninther,'' median of three medians of three. 81 s := (hi - lo) / 8 82 medianOfThree(data, lo, lo+s, lo+2*s) 83 medianOfThree(data, m, m-s, m+s) 84 medianOfThree(data, hi-1, hi-1-s, hi-1-2*s) 85 } 86 medianOfThree(data, lo, m, hi-1) 87 88 // Invariants are: 89 // data[lo] = pivot (set up by ChoosePivot) 90 // data[lo < i < a] < pivot 91 // data[a <= i < b] <= pivot 92 // data[b <= i < c] unexamined 93 // data[c <= i < hi-1] > pivot 94 // data[hi-1] >= pivot 95 pivot := lo 96 a, c := lo+1, hi-1 97 98 for ; a < c && data[a] < data[pivot]; a++ { 99 } 100 b := a 101 for { 102 for ; b < c && !(data[pivot] < data[b]); b++ { // data[b] <= pivot 103 } 104 for ; b < c && data[pivot] < data[c-1]; c-- { // data[c-1] > pivot 105 } 106 if b >= c { 107 break 108 } 109 // data[b] > pivot; data[c-1] <= pivot 110 data[b], data[c-1] = data[c-1], data[b] 111 b++ 112 c-- 113 } 114 // If hi-c<3 then there are duplicates (by property of median of nine). 115 // Let be a bit more conservative, and set border to 5. 116 protect := hi-c < 5 117 if !protect && hi-c < (hi-lo)/4 { 118 // Lets test some points for equality to pivot 119 dups := 0 120 if !(data[pivot] < data[hi-1]) { // data[hi-1] = pivot 121 data[c], data[hi-1] = data[hi-1], data[c] 122 c++ 123 dups++ 124 } 125 if !(data[b-1] < data[pivot]) { // data[b-1] = pivot 126 b-- 127 dups++ 128 } 129 // m-lo = (hi-lo)/2 > 6 130 // b-lo > (hi-lo)*3/4-1 > 8 131 // ==> m < b ==> data[m] <= pivot 132 if !(data[m] < data[pivot]) { // data[m] = pivot 133 data[m], data[b-1] = data[b-1], data[m] 134 b-- 135 dups++ 136 } 137 // if at least 2 points are equal to pivot, assume skewed distribution 138 protect = dups > 1 139 } 140 if protect { 141 // Protect against a lot of duplicates 142 // Add invariant: 143 // data[a <= i < b] unexamined 144 // data[b <= i < c] = pivot 145 for { 146 for ; a < b && !(data[b-1] < data[pivot]); b-- { // data[b] == pivot 147 } 148 for ; a < b && data[a] < data[pivot]; a++ { // data[a] < pivot 149 } 150 if a >= b { 151 break 152 } 153 // data[a] == pivot; data[b-1] < pivot 154 data[a], data[b-1] = data[b-1], data[a] 155 a++ 156 b-- 157 } 158 } 159 // Swap pivot into middle 160 data[pivot], data[b-1] = data[b-1], data[pivot] 161 return b - 1, c 162 } 163 164 func qSort(data []int, a, b, maxDepth int) { 165 for b-a > 12 { // Use ShellSort for slices <= 12 elements 166 if maxDepth == 0 { 167 heapSort(data, a, b) 168 return 169 } 170 maxDepth-- 171 mlo, mhi := doPivot(data, a, b) 172 // Avoiding recursion on the larger subproblem guarantees 173 // a stack depth of at most lg(b-a). 174 if mlo-a < b-mhi { 175 qSort(data, a, mlo, maxDepth) 176 a = mhi // i.e., quickSort(data, mhi, b) 177 } else { 178 qSort(data, mhi, b, maxDepth) 179 b = mlo // i.e., quickSort(data, a, mlo) 180 } 181 } 182 if b-a > 1 { 183 // Do ShellSort pass with gap 6 184 // It could be written in this simplified form cause b-a <= 12 185 for i := a + 6; i < b; i++ { 186 if data[i] < data[i-6] { 187 data[i], data[i-6] = data[i-6], data[i] 188 } 189 } 190 insertionSort(data, a, b) 191 } 192 } 193 194 // Sort sorts data. 195 // It makes one call to data.Len to determine n, and O(n*log(n)) calls to 196 // data.Less and data.Swap. The sort is not guaranteed to be stable. 197 func stdSort(data []int, start, end int, ch chan bool) { 198 n := len(data) 199 mid := n / 2 200 qSort(data, start, end, maxDepth(mid)) 201 ch <- true 202 } 203 204 func StdSortWithoutInterface(data []int) { 205 n := len(data) 206 mid := n / 2 207 qSort(data, 0, n, maxDepth(mid)) 208 } 209 210 func maxDepth(n int) int { 211 var depth int 212 for i := n; i > 0; i >>= 1 { 213 depth++ 214 } 215 return depth * 2 216 } 217 218 func StdSortWithGoroutine(data []int) { 219 n := len(data) 220 mid := n / 2 221 222 chs := make(chan bool, 2) 223 224 go stdSort(data, 0, mid, chs) 225 go stdSort(data, mid, n, chs) 226 227 for i := 0; i < 2; i++ { 228 <-chs 229 } 230 res := make([]int, n) 231 mergeArray(data, 0, mid-1, n-1, res) 232 }