2008-09-08 19:43:42 -06:00
|
|
|
// 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.
|
|
|
|
|
2009-03-06 18:29:25 -07:00
|
|
|
// The sort package provides primitives for sorting arrays
|
|
|
|
// and user-defined collections.
|
2008-11-17 12:51:34 -07:00
|
|
|
package sort
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
// A type, typically a collection, that satisfies sort.Interface can be
|
|
|
|
// sorted by the routines in this package. The methods require that the
|
|
|
|
// elements of the collection be enumerated by an integer index.
|
|
|
|
type Interface interface {
|
2009-03-06 18:29:25 -07:00
|
|
|
// Len is the number of elements in the collection.
|
2008-11-19 15:32:15 -07:00
|
|
|
Len() int;
|
2009-03-06 18:29:25 -07:00
|
|
|
// Less returns whether the element with index i is should sort
|
|
|
|
// before the element with index j.
|
2008-11-19 15:32:15 -07:00
|
|
|
Less(i, j int) bool;
|
2009-03-06 18:29:25 -07:00
|
|
|
// Swap swaps the elements with indexes i and j.
|
2008-11-19 15:32:15 -07:00
|
|
|
Swap(i, j int);
|
2008-09-08 19:43:42 -06:00
|
|
|
}
|
|
|
|
|
2008-11-17 12:51:34 -07:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a;
|
2008-09-08 19:43:42 -06:00
|
|
|
}
|
2008-11-17 12:51:34 -07:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insertion sort
|
2009-10-13 14:05:16 -06:00
|
|
|
func insertionSort(data Interface, a, b int) {
|
2008-11-17 12:51:34 -07:00
|
|
|
for i := a+1; i < b; i++ {
|
2008-11-19 15:32:15 -07:00
|
|
|
for j := i; j > a && data.Less(j, j-1); j-- {
|
|
|
|
data.Swap(j, j-1);
|
2008-09-08 19:43:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 12:51:34 -07:00
|
|
|
// Quicksort, following Bentley and McIlroy,
|
|
|
|
// ``Engineering a Sort Function,'' SP&E November 1993.
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2008-11-17 12:51:34 -07:00
|
|
|
// Move the median of the three values data[a], data[b], data[c] into data[a].
|
2009-10-13 14:05:16 -06:00
|
|
|
func medianOfThree(data Interface, a, b, c int) {
|
2008-11-17 12:51:34 -07:00
|
|
|
m0 := b;
|
|
|
|
m1 := a;
|
|
|
|
m2 := c;
|
|
|
|
// bubble sort on 3 elements
|
2009-10-07 12:55:06 -06:00
|
|
|
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);
|
|
|
|
}
|
2009-11-05 00:25:29 -07:00
|
|
|
// now data[m0] <= data[m1] <= data[m2]
|
2008-11-17 12:51:34 -07:00
|
|
|
}
|
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
func swapRange(data Interface, a, b, n int) {
|
2008-11-17 12:51:34 -07:00
|
|
|
for i := 0; i < n; i++ {
|
2008-11-19 15:32:15 -07:00
|
|
|
data.Swap(a+i, b+i);
|
2008-09-08 19:43:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
func doPivot(data Interface, lo, hi int) (midlo, midhi int) {
|
2008-11-17 12:51:34 -07:00
|
|
|
m := (lo+hi)/2;
|
2009-10-07 12:55:06 -06:00
|
|
|
if hi-lo > 40 {
|
2008-11-17 12:51:34 -07:00
|
|
|
// Tukey's ``Ninther,'' median of three medians of three.
|
2009-10-07 12:55:06 -06:00
|
|
|
s := (hi-lo)/8;
|
|
|
|
medianOfThree(data, lo, lo+s, lo + 2*s);
|
2009-01-16 11:34:21 -07:00
|
|
|
medianOfThree(data, m, m-s, m+s);
|
2009-10-07 12:55:06 -06:00
|
|
|
medianOfThree(data, hi-1, hi-1-s, hi - 1 - 2*s);
|
2008-11-17 12:51:34 -07:00
|
|
|
}
|
2009-01-16 11:34:21 -07:00
|
|
|
medianOfThree(data, lo, m, hi-1);
|
2008-11-17 12:51:34 -07:00
|
|
|
|
|
|
|
// 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 {
|
2008-11-19 15:32:15 -07:00
|
|
|
if data.Less(b, pivot) { // data[b] < pivot
|
2008-11-17 12:51:34 -07:00
|
|
|
b++;
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-19 15:32:15 -07:00
|
|
|
if !data.Less(pivot, b) { // data[b] = pivot
|
|
|
|
data.Swap(a, b);
|
2008-11-17 12:51:34 -07:00
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-19 15:32:15 -07:00
|
|
|
if data.Less(pivot, c-1) { // data[c-1] > pivot
|
2008-11-17 12:51:34 -07:00
|
|
|
c--;
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-19 15:32:15 -07:00
|
|
|
if !data.Less(c-1, pivot) { // data[c-1] = pivot
|
|
|
|
data.Swap(c-1, d-1);
|
2008-11-17 12:51:34 -07:00
|
|
|
c--;
|
|
|
|
d--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// data[b] > pivot; data[c-1] < pivot
|
2008-11-19 15:32:15 -07:00
|
|
|
data.Swap(b, c-1);
|
2008-11-17 12:51:34 -07:00
|
|
|
b++;
|
|
|
|
c--;
|
|
|
|
}
|
|
|
|
|
|
|
|
n := min(b-a, a-lo);
|
2009-01-16 11:34:21 -07:00
|
|
|
swapRange(data, lo, b-n, n);
|
2008-11-17 12:51:34 -07:00
|
|
|
|
|
|
|
n = min(hi-d, d-c);
|
2009-01-16 11:34:21 -07:00
|
|
|
swapRange(data, c, hi-n, n);
|
2008-11-17 12:51:34 -07:00
|
|
|
|
|
|
|
return lo+b-a, hi-(d-c);
|
|
|
|
}
|
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
func quickSort(data Interface, a, b int) {
|
2009-10-07 12:55:06 -06:00
|
|
|
if b-a > 7 {
|
2009-01-16 11:34:21 -07:00
|
|
|
mlo, mhi := doPivot(data, a, b);
|
|
|
|
quickSort(data, a, mlo);
|
|
|
|
quickSort(data, mhi, b);
|
2009-10-07 12:55:06 -06:00
|
|
|
} else if b-a > 1 {
|
2009-01-16 11:34:21 -07:00
|
|
|
insertionSort(data, a, b);
|
2008-11-17 12:51:34 -07:00
|
|
|
}
|
|
|
|
}
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
func Sort(data Interface) {
|
2009-01-16 11:34:21 -07:00
|
|
|
quickSort(data, 0, data.Len());
|
2008-09-08 19:43:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
func IsSorted(data Interface) bool {
|
2008-11-19 15:32:15 -07:00
|
|
|
n := data.Len();
|
2009-10-07 12:55:06 -06:00
|
|
|
for i := n-1; i > 0; i-- {
|
|
|
|
if data.Less(i, i-1) {
|
2008-09-08 19:43:42 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Convenience types for common cases
|
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
// IntArray attaches the methods of Interface to []int, sorting in increasing order.
|
2009-01-20 15:40:40 -07:00
|
|
|
type IntArray []int
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
func (p IntArray) Len() int {
|
|
|
|
return len(p);
|
|
|
|
}
|
|
|
|
func (p IntArray) Less(i, j int) bool {
|
|
|
|
return p[i] < p[j];
|
|
|
|
}
|
|
|
|
func (p IntArray) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i];
|
|
|
|
}
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-08-27 19:23:45 -06:00
|
|
|
// Sort is a convenience method.
|
2009-10-07 12:55:06 -06:00
|
|
|
func (p IntArray) Sort() {
|
|
|
|
Sort(p);
|
|
|
|
}
|
2009-08-27 19:23:45 -06:00
|
|
|
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
// FloatArray attaches the methods of Interface to []float, sorting in increasing order.
|
2009-10-07 12:55:06 -06:00
|
|
|
type FloatArray []float
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
func (p FloatArray) Len() int {
|
|
|
|
return len(p);
|
|
|
|
}
|
|
|
|
func (p FloatArray) Less(i, j int) bool {
|
|
|
|
return p[i] < p[j];
|
|
|
|
}
|
|
|
|
func (p FloatArray) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i];
|
|
|
|
}
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-08-27 19:23:45 -06:00
|
|
|
// Sort is a convenience method.
|
2009-10-07 12:55:06 -06:00
|
|
|
func (p FloatArray) Sort() {
|
|
|
|
Sort(p);
|
|
|
|
}
|
2009-08-27 19:23:45 -06:00
|
|
|
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-10-13 14:05:16 -06:00
|
|
|
// StringArray attaches the methods of Interface to []string, sorting in increasing order.
|
2009-01-20 15:40:40 -07:00
|
|
|
type StringArray []string
|
2008-09-08 19:43:42 -06:00
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
func (p StringArray) Len() int {
|
|
|
|
return len(p);
|
|
|
|
}
|
|
|
|
func (p StringArray) Less(i, j int) bool {
|
|
|
|
return p[i] < p[j];
|
|
|
|
}
|
|
|
|
func (p StringArray) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i];
|
|
|
|
}
|
2008-09-09 19:13:08 -06:00
|
|
|
|
2009-08-27 19:23:45 -06:00
|
|
|
// Sort is a convenience method.
|
2009-10-07 12:55:06 -06:00
|
|
|
func (p StringArray) Sort() {
|
|
|
|
Sort(p);
|
|
|
|
}
|
2009-08-27 19:23:45 -06:00
|
|
|
|
2008-09-09 19:13:08 -06:00
|
|
|
|
|
|
|
// Convenience wrappers for common cases
|
|
|
|
|
2009-03-06 18:29:25 -07:00
|
|
|
// SortInts sorts an array of ints in increasing order.
|
2009-10-07 12:55:06 -06:00
|
|
|
func SortInts(a []int) {
|
|
|
|
Sort(IntArray(a));
|
|
|
|
}
|
2009-03-06 18:29:25 -07:00
|
|
|
// SortFloats sorts an array of floats in increasing order.
|
2009-10-07 12:55:06 -06:00
|
|
|
func SortFloats(a []float) {
|
|
|
|
Sort(FloatArray(a));
|
|
|
|
}
|
2009-03-06 18:29:25 -07:00
|
|
|
// SortStrings sorts an array of strings in increasing order.
|
2009-10-07 12:55:06 -06:00
|
|
|
func SortStrings(a []string) {
|
|
|
|
Sort(StringArray(a));
|
|
|
|
}
|
2008-09-09 19:13:08 -06:00
|
|
|
|
|
|
|
|
2009-03-06 18:29:25 -07:00
|
|
|
// IntsAreSorted tests whether an array of ints is sorted in increasing order.
|
2009-10-07 12:55:06 -06:00
|
|
|
func IntsAreSorted(a []int) bool {
|
|
|
|
return IsSorted(IntArray(a));
|
|
|
|
}
|
2009-03-06 18:29:25 -07:00
|
|
|
// FloatsAreSorted tests whether an array of floats is sorted in increasing order.
|
2009-10-07 12:55:06 -06:00
|
|
|
func FloatsAreSorted(a []float) bool {
|
|
|
|
return IsSorted(FloatArray(a));
|
|
|
|
}
|
2009-03-06 18:29:25 -07:00
|
|
|
// StringsAreSorted tests whether an array of strings is sorted in increasing order.
|
2009-10-07 12:55:06 -06:00
|
|
|
func StringsAreSorted(a []string) bool {
|
|
|
|
return IsSorted(StringArray(a));
|
|
|
|
}
|