diff --git a/src/lib/sort.go b/src/lib/sort.go index 39a5f35925..45726d80a6 100644 --- a/src/lib/sort.go +++ b/src/lib/sort.go @@ -18,7 +18,7 @@ func min(a, b int) int { } // Insertion sort -func InsertionSort(data SortInterface, a, b int) { +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); @@ -30,7 +30,7 @@ func InsertionSort(data SortInterface, a, b int) { // ``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) { +func medianOfThree(data SortInterface, a, b, c int) { m0 := b; m1 := a; m2 := c; @@ -41,22 +41,22 @@ func MedianOfThree(data SortInterface, a, b, c int) { // now data[m0] <= data[m1] <= data[m2] } -func SwapRange(data SortInterface, a, b, n int) { +func swapRange(data SortInterface, a, b, n int) { for i := 0; i < n; i++ { data.Swap(a+i, b+i); } } -func Pivot(data SortInterface, lo, hi int) (midlo, midhi int) { +func doPivot(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, 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); + medianOfThree(data, lo, m, hi-1); // Invariants are: // data[lo] = pivot (set up by ChoosePivot) @@ -98,26 +98,26 @@ func Pivot(data SortInterface, lo, hi int) (midlo, midhi int) { } n := min(b-a, a-lo); - SwapRange(data, lo, b-n, n); + swapRange(data, lo, b-n, n); n = min(hi-d, d-c); - SwapRange(data, c, hi-n, n); + swapRange(data, c, hi-n, n); return lo+b-a, hi-(d-c); } -func Quicksort(data SortInterface, a, b int) { +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); + mlo, mhi := doPivot(data, a, b); + quickSort(data, a, mlo); + quickSort(data, mhi, b); } else if b - a > 1 { - InsertionSort(data, a, b); + insertionSort(data, a, b); } } export func Sort(data SortInterface) { - Quicksort(data, 0, data.Len()); + quickSort(data, 0, data.Len()); } diff --git a/src/lib/sort_test.go b/src/lib/sort_test.go index 6684d93dab..2a8b88c577 100644 --- a/src/lib/sort_test.go +++ b/src/lib/sort_test.go @@ -11,8 +11,6 @@ import ( "testing"; ) -func BentleyMcIlroyTests(); - var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586} var floats = [...]float{74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8} @@ -75,7 +73,7 @@ export func TestSortStrings(t *testing.T) { } } -export func TestSortLargeRandom(t *testing.T) { +export func TestSortLarge_Random(t *testing.T) { data := make([]int, 1000000); for i := 0; i < len(data); i++ { data[i] = rand.rand() % 100; @@ -90,25 +88,25 @@ export func TestSortLargeRandom(t *testing.T) { } const ( - Sawtooth = iota; - Rand; - Stagger; - Plateau; - Shuffle; - NDist; + _Sawtooth = iota; + _Rand; + _Stagger; + _Plateau; + _Shuffle; + _NDist; ) const ( - Copy = iota; - Reverse; - ReverseFirstHalf; - ReverseSecondHalf; - Sorted; - Dither; - NMode; -); + _Copy = iota; + _Reverse; + _ReverseFirstHalf; + _ReverseSecondHalf; + _Sorted; + _Dither; + _NMode; +) -type TestingData struct { +type testingData struct { desc string; t *testing.T; data []int; @@ -116,9 +114,9 @@ type TestingData struct { nswap int; } -func (d *TestingData) Len() int { return len(d.data); } -func (d *TestingData) Less(i, j int) bool { return d.data[i] < d.data[j]; } -func (d *TestingData) Swap(i, j int) { +func (d *testingData) Len() int { return len(d.data); } +func (d *testingData) Less(i, j int) bool { return d.data[i] < d.data[j]; } +func (d *testingData) Swap(i, j int) { if d.nswap >= d.maxswap { d.t.Errorf("%s: used %d swaps sorting array of %d", d.desc, d.nswap, len(d.data)); d.t.FailNow(); @@ -127,7 +125,7 @@ func (d *TestingData) Swap(i, j int) { d.data[i], d.data[j] = d.data[j], d.data[i]; } -func Lg(n int) int { +func lg(n int) int { i := 0; for 1< 0 && t.errors[l-1] != '\n' { t.errors += "\n" @@ -74,7 +74,7 @@ export type Test struct { f *(*T); } -func TRunner(t *T, test *Test) { +func tRunner(t *T, test *Test) { test.f(t); t.ch <- t; } @@ -91,7 +91,7 @@ export func Main(tests []Test) { } t := new(T); t.ch = make(chan *T); - go TRunner(t, &tests[i]); + go tRunner(t, &tests[i]); <-t.ch; if t.failed { println("--- FAIL:", tests[i].name);