2010-12-03 05:34:57 +01: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.
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Package sort provides primitives for sorting slices and user-defined
|
2011-05-20 02:18:15 +02:00
|
|
|
// collections.
|
2010-12-03 05:34:57 +01:00
|
|
|
package sort
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
import "math"
|
|
|
|
|
2010-12-03 05:34:57 +01: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 {
|
|
|
|
// Len is the number of elements in the collection.
|
|
|
|
Len() int
|
|
|
|
// Less returns whether the element with index i should sort
|
|
|
|
// before the element with index j.
|
|
|
|
Less(i, j int) bool
|
|
|
|
// Swap swaps the elements with indexes i and j.
|
|
|
|
Swap(i, j int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insertion sort
|
|
|
|
func insertionSort(data Interface, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// siftDown implements the heap property on data[lo, hi).
|
|
|
|
// first is an offset into the array where the root of the heap lies.
|
|
|
|
func siftDown(data Interface, lo, hi, first int) {
|
|
|
|
root := lo
|
|
|
|
for {
|
|
|
|
child := 2*root + 1
|
|
|
|
if child >= hi {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if child+1 < hi && data.Less(first+child, first+child+1) {
|
|
|
|
child++
|
|
|
|
}
|
|
|
|
if !data.Less(first+root, first+child) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data.Swap(first+root, first+child)
|
|
|
|
root = child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func heapSort(data Interface, a, b int) {
|
|
|
|
first := a
|
|
|
|
lo := 0
|
|
|
|
hi := b - a
|
|
|
|
|
|
|
|
// Build heap with greatest element at top.
|
|
|
|
for i := (hi - 1) / 2; i >= 0; i-- {
|
|
|
|
siftDown(data, i, hi, first)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop elements, largest first, into end of data.
|
|
|
|
for i := hi - 1; i >= 0; i-- {
|
|
|
|
data.Swap(first, first+i)
|
|
|
|
siftDown(data, lo, i, first)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Quicksort, following Bentley and McIlroy,
|
|
|
|
// ``Engineering a Sort Function,'' SP&E November 1993.
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// medianOfThree moves the median of the three values data[a], data[b], data[c] into data[a].
|
2010-12-03 05:34:57 +01:00
|
|
|
func medianOfThree(data Interface, 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 Interface, a, b, n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data.Swap(a+i, b+i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doPivot(data Interface, lo, hi int) (midlo, midhi int) {
|
2011-01-21 19:19:03 +01:00
|
|
|
m := lo + (hi-lo)/2 // Written like this to avoid integer overflow.
|
2010-12-03 05:34:57 +01:00
|
|
|
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
|
2011-09-16 17:47:21 +02:00
|
|
|
// into the middle of the slice.
|
2010-12-03 05:34:57 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
func quickSort(data Interface, a, b, maxDepth int) {
|
2011-01-21 19:19:03 +01:00
|
|
|
for b-a > 7 {
|
2011-10-27 01:57:58 +02:00
|
|
|
if maxDepth == 0 {
|
|
|
|
heapSort(data, a, b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
maxDepth--
|
2010-12-03 05:34:57 +01:00
|
|
|
mlo, mhi := doPivot(data, a, b)
|
2011-01-21 19:19:03 +01:00
|
|
|
// Avoiding recursion on the larger subproblem guarantees
|
|
|
|
// a stack depth of at most lg(b-a).
|
|
|
|
if mlo-a < b-mhi {
|
2011-10-27 01:57:58 +02:00
|
|
|
quickSort(data, a, mlo, maxDepth)
|
2011-01-21 19:19:03 +01:00
|
|
|
a = mhi // i.e., quickSort(data, mhi, b)
|
|
|
|
} else {
|
2011-10-27 01:57:58 +02:00
|
|
|
quickSort(data, mhi, b, maxDepth)
|
2011-01-21 19:19:03 +01:00
|
|
|
b = mlo // i.e., quickSort(data, a, mlo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b-a > 1 {
|
2010-12-03 05:34:57 +01:00
|
|
|
insertionSort(data, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-31 00:09:55 +02:00
|
|
|
// Sort sorts data.
|
|
|
|
// It makes one call to data.Len to determine n, and O(n*log(n)) calls to
|
|
|
|
// data.Less and data.Swap. The sort is not guaranteed to be stable.
|
2011-10-27 01:57:58 +02:00
|
|
|
func Sort(data Interface) {
|
2012-03-31 00:09:55 +02:00
|
|
|
// Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached.
|
2011-10-27 01:57:58 +02:00
|
|
|
n := data.Len()
|
|
|
|
maxDepth := 0
|
2012-03-31 00:09:55 +02:00
|
|
|
for i := n; i > 0; i >>= 1 {
|
2011-10-27 01:57:58 +02:00
|
|
|
maxDepth++
|
|
|
|
}
|
|
|
|
maxDepth *= 2
|
2012-01-25 21:56:26 +01:00
|
|
|
quickSort(data, 0, n, maxDepth)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-03-31 00:09:55 +02:00
|
|
|
// IsSorted reports whether data is sorted.
|
2010-12-03 05:34:57 +01:00
|
|
|
func IsSorted(data Interface) 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
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
|
|
|
|
type IntSlice []int
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func (p IntSlice) Len() int { return len(p) }
|
|
|
|
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
|
|
|
|
func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Sort is a convenience method.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (p IntSlice) Sort() { Sort(p) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Float64Slice attaches the methods of Interface to []float64, sorting in increasing order.
|
|
|
|
type Float64Slice []float64
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func (p Float64Slice) Len() int { return len(p) }
|
|
|
|
func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] || math.IsNaN(p[i]) && !math.IsNaN(p[j]) }
|
|
|
|
func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Sort is a convenience method.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (p Float64Slice) Sort() { Sort(p) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
|
|
|
|
type StringSlice []string
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func (p StringSlice) Len() int { return len(p) }
|
|
|
|
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
|
|
|
|
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Sort is a convenience method.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (p StringSlice) Sort() { Sort(p) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Convenience wrappers for common cases
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Ints sorts a slice of ints in increasing order.
|
|
|
|
func Ints(a []int) { Sort(IntSlice(a)) }
|
2012-01-13 06:11:45 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Float64s sorts a slice of float64s in increasing order.
|
|
|
|
func Float64s(a []float64) { Sort(Float64Slice(a)) }
|
2012-01-13 06:11:45 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Strings sorts a slice of strings in increasing order.
|
|
|
|
func Strings(a []string) { Sort(StringSlice(a)) }
|
|
|
|
|
|
|
|
// IntsAreSorted tests whether a slice of ints is sorted in increasing order.
|
|
|
|
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
|
2012-01-13 06:11:45 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Float64sAreSorted tests whether a slice of float64s is sorted in increasing order.
|
|
|
|
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
|
2012-01-13 06:11:45 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// StringsAreSorted tests whether a slice of strings is sorted in increasing order.
|
|
|
|
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
|