Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 1 | // Copyright 2010 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // This file implements binary search. |
| 6 | |
| 7 | package sort |
| 8 | |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 9 | // Search uses binary search to find and return the smallest index i |
Robert Griesemer | 465b9c3 | 2012-10-30 13:38:01 -0700 | [diff] [blame] | 10 | // in [0, n) at which f(i) is true, assuming that on the range [0, n), |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 11 | // f(i) == true implies f(i+1) == true. That is, Search requires that |
| 12 | // f is false for some (possibly empty) prefix of the input range [0, n) |
| 13 | // and then true for the (possibly empty) remainder; Search returns |
| 14 | // the first true index. If there is no such index, Search returns n. |
Shenghou Ma | 882eb60 | 2012-11-07 05:07:46 +0800 | [diff] [blame] | 15 | // (Note that the "not found" return value is not -1 as in, for instance, |
| 16 | // strings.Index). |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 17 | // Search calls f(i) only for i in the range [0, n). |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 18 | // |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 19 | // A common use of Search is to find the index i for a value x in |
Rob Pike | 2b08e95 | 2011-06-16 17:48:02 +1000 | [diff] [blame] | 20 | // a sorted, indexable data structure such as an array or slice. |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 21 | // In this case, the argument f, typically a closure, captures the value |
| 22 | // to be searched for, and how the data structure is indexed and |
| 23 | // ordered. |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 24 | // |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 25 | // For instance, given a slice data sorted in ascending order, |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 26 | // the call Search(len(data), func(i int) bool { return data[i] >= 23 }) |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 27 | // returns the smallest index i such that data[i] >= 23. If the caller |
| 28 | // wants to find whether 23 is in the slice, it must test data[i] == 23 |
| 29 | // separately. |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 30 | // |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 31 | // Searching data sorted in descending order would use the <= |
| 32 | // operator instead of the >= operator. |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 33 | // |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 34 | // To complete the example above, the following code tries to find the value |
| 35 | // x in an integer slice data sorted in ascending order: |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 36 | // |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 37 | // x := 23 |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 38 | // i := sort.Search(len(data), func(i int) bool { return data[i] >= x }) |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 39 | // if i < len(data) && data[i] == x { |
| 40 | // // x is present at data[i] |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 41 | // } else { |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 42 | // // x is not present in data, |
| 43 | // // but i is the index where it would be inserted. |
| 44 | // } |
| 45 | // |
| 46 | // As a more whimsical example, this program guesses your number: |
| 47 | // |
| 48 | // func GuessingGame() { |
| 49 | // var s string |
| 50 | // fmt.Printf("Pick an integer from 0 to 100.\n") |
| 51 | // answer := sort.Search(100, func(i int) bool { |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 52 | // fmt.Printf("Is your number <= %d? ", i) |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 53 | // fmt.Scanf("%s", &s) |
| 54 | // return s != "" && s[0] == 'y' |
| 55 | // }) |
| 56 | // fmt.Printf("Your number is %d.\n", answer) |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 57 | // } |
Robert Griesemer | 8f651ff | 2010-11-12 16:08:56 -0800 | [diff] [blame] | 58 | // |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 59 | func Search(n int, f func(int) bool) int { |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 60 | // Define f(-1) == false and f(n) == true. |
| 61 | // Invariant: f(i-1) == false, f(j) == true. |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 62 | i, j := 0, n |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 63 | for i < j { |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 64 | h := i + (j-i)/2 // avoid overflow when computing h |
Russ Cox | 19f0e46 | 2010-11-18 07:16:09 -0500 | [diff] [blame] | 65 | // i ≤ h < j |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 66 | if !f(h) { |
| 67 | i = h + 1 // preserves f(i-1) == false |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 68 | } else { |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 69 | j = h // preserves f(j) == true |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 72 | // i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i. |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 73 | return i |
| 74 | } |
| 75 | |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 76 | // Convenience wrappers for common cases. |
| 77 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 78 | // SearchInts searches for x in a sorted slice of ints and returns the index |
Shenghou Ma | 882eb60 | 2012-11-07 05:07:46 +0800 | [diff] [blame] | 79 | // as specified by Search. The return value is the index to insert x if x is |
| 80 | // not present (it could be len(a)). |
Rob Pike | 20548b1 | 2012-11-02 16:17:34 -0700 | [diff] [blame] | 81 | // The slice must be sorted in ascending order. |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 82 | // |
| 83 | func SearchInts(a []int, x int) int { |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 84 | return Search(len(a), func(i int) bool { return a[i] >= x }) |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 87 | // SearchFloat64s searches for x in a sorted slice of float64s and returns the index |
Shenghou Ma | 882eb60 | 2012-11-07 05:07:46 +0800 | [diff] [blame] | 88 | // as specified by Search. The return value is the index to insert x if x is not |
| 89 | // present (it could be len(a)). |
Rob Pike | 20548b1 | 2012-11-02 16:17:34 -0700 | [diff] [blame] | 90 | // The slice must be sorted in ascending order. |
Robert Griesemer | 465b9c3 | 2012-10-30 13:38:01 -0700 | [diff] [blame] | 91 | // |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 92 | func SearchFloat64s(a []float64, x float64) int { |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 93 | return Search(len(a), func(i int) bool { return a[i] >= x }) |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Patrick Smith | 3c80816 | 2012-10-28 10:07:59 +1100 | [diff] [blame] | 96 | // SearchStrings searches for x in a sorted slice of strings and returns the index |
Shenghou Ma | 882eb60 | 2012-11-07 05:07:46 +0800 | [diff] [blame] | 97 | // as specified by Search. The return value is the index to insert x if x is not |
| 98 | // present (it could be len(a)). |
Rob Pike | 20548b1 | 2012-11-02 16:17:34 -0700 | [diff] [blame] | 99 | // The slice must be sorted in ascending order. |
Robert Griesemer | 465b9c3 | 2012-10-30 13:38:01 -0700 | [diff] [blame] | 100 | // |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 101 | func SearchStrings(a []string, x string) int { |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 102 | return Search(len(a), func(i int) bool { return a[i] >= x }) |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 105 | // Search returns the result of applying SearchInts to the receiver and x. |
Rob Pike | 4b1170d | 2011-06-11 09:25:18 +1000 | [diff] [blame] | 106 | func (p IntSlice) Search(x int) int { return SearchInts(p, x) } |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 107 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 108 | // Search returns the result of applying SearchFloat64s to the receiver and x. |
Rob Pike | 2b08e95 | 2011-06-16 17:48:02 +1000 | [diff] [blame] | 109 | func (p Float64Slice) Search(x float64) int { return SearchFloat64s(p, x) } |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 110 | |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 111 | // Search returns the result of applying SearchStrings to the receiver and x. |
Rob Pike | 4b1170d | 2011-06-11 09:25:18 +1000 | [diff] [blame] | 112 | func (p StringSlice) Search(x string) int { return SearchStrings(p, x) } |