Newer
Older
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
export type SortInterface interface {
len() int;
less(i, j int) bool;
swap(i, j int);
}
func min(a, b int) int {
if a < b {
return a;
return b;
}
// Insertion sort
func InsertionSort(data SortInterface, a, b int) {
for i := a+1; i < b; i++ {
for j := i; j > a && data.less(j, j-1); j-- {
data.swap(j, j-1);
// Quicksort, following Bentley and McIlroy,
// ``Engineering a Sort Function,'' SP&E November 1993.
// Move the median of the three values data[a], data[b], data[c] into data[a].
func MedianOfThree(data SortInterface, a, b, c int) {
m0 := b;
m1 := a;
m2 := c;
// bubble sort on 3 elements
if data.less(m1, m0) { data.swap(m1, m0); }
if data.less(m2, m1) { data.swap(m2, m1); }
if data.less(m1, m0) { data.swap(m1, m0); }
// now data[m0] <= data[m1] <= data[m2]
}
func SwapRange(data SortInterface, a, b, n int) {
for i := 0; i < n; i++ {
data.swap(a+i, b+i);
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
func Pivot(data SortInterface, lo, hi int) (midlo, midhi int) {
m := (lo+hi)/2;
if hi - lo > 40 {
// Tukey's ``Ninther,'' median of three medians of three.
s := (hi - lo) / 8;
MedianOfThree(data, lo, lo+s, lo+2*s);
MedianOfThree(data, m, m-s, m+s);
MedianOfThree(data, hi-1, hi-1-s, hi-1-2*s);
}
MedianOfThree(data, lo, m, hi-1);
// Invariants are:
// data[lo] = pivot (set up by ChoosePivot)
// data[lo <= i < a] = pivot
// data[a <= i < b] < pivot
// data[b <= i < c] is unexamined
// data[c <= i < d] > pivot
// data[d <= i < hi] = pivot
//
// Once b meets c, can swap the "= pivot" sections
// into the middle of the array.
pivot := lo;
a, b, c, d := lo+1, lo+1, hi, hi;
for b < c {
if data.less(b, pivot) { // data[b] < pivot
b++;
continue;
}
if !data.less(pivot, b) { // data[b] = pivot
data.swap(a, b);
a++;
b++;
continue;
}
if data.less(pivot, c-1) { // data[c-1] > pivot
c--;
continue;
}
if !data.less(c-1, pivot) { // data[c-1] = pivot
data.swap(c-1, d-1);
c--;
d--;
continue;
}
// data[b] > pivot; data[c-1] < pivot
data.swap(b, c-1);
b++;
c--;
}
n := min(b-a, a-lo);
SwapRange(data, lo, b-n, n);
n = min(hi-d, d-c);
SwapRange(data, c, hi-n, n);
return lo+b-a, hi-(d-c);
}
func Quicksort(data SortInterface, a, b int) {
if b - a > 7 {
mlo, mhi := Pivot(data, a, b);
Quicksort(data, a, mlo);
Quicksort(data, mhi, b);
} else if b - a > 1 {
InsertionSort(data, a, b);
}
}
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
export func Sort(data SortInterface) {
Quicksort(data, 0, data.len());
}
export func IsSorted(data SortInterface) bool {
n := data.len();
for i := n - 1; i > 0; i-- {
if data.less(i, i - 1) {
return false;
}
}
return true;
}
// Convenience types for common cases
export type IntArray struct {
data *[]int;
}
func (p *IntArray) len() int { return len(p.data); }
func (p *IntArray) less(i, j int) bool { return p.data[i] < p.data[j]; }
func (p *IntArray) swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i]; }
export type FloatArray struct {
data *[]float;
}
func (p *FloatArray) len() int { return len(p.data); }
func (p *FloatArray) less(i, j int) bool { return p.data[i] < p.data[j]; }
func (p *FloatArray) swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i]; }
export type StringArray struct {
data *[]string;
}
func (p *StringArray) len() int { return len(p.data); }
func (p *StringArray) less(i, j int) bool { return p.data[i] < p.data[j]; }
func (p *StringArray) swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i]; }
// Convenience wrappers for common cases
export func SortInts(a *[]int) { Sort(&IntArray{a}); }
export func SortFloats(a *[]float) { Sort(&FloatArray{a}); }
export func SortStrings(a *[]string) { Sort(&StringArray{a}); }
export func IntsAreSorted(a *[]int) bool { return IsSorted(&IntArray{a}); }
export func FloatsAreSorted(a *[]float) bool { return IsSorted(&FloatArray{a}); }
export func StringsAreSorted(a *[]string) bool { return IsSorted(&StringArray{a}); }