...

Source file src/sort/example_search_test.go

Documentation: sort

     1  // Copyright 2016 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  package sort_test
     6  
     7  import (
     8  	"fmt"
     9  	"sort"
    10  	"strings"
    11  )
    12  
    13  // This example demonstrates searching a list sorted in ascending order.
    14  func ExampleSearch() {
    15  	a := []int{1, 3, 6, 10, 15, 21, 28, 36, 45, 55}
    16  	x := 6
    17  
    18  	i := sort.Search(len(a), func(i int) bool { return a[i] >= x })
    19  	if i < len(a) && a[i] == x {
    20  		fmt.Printf("found %d at index %d in %v\n", x, i, a)
    21  	} else {
    22  		fmt.Printf("%d not found in %v\n", x, a)
    23  	}
    24  	// Output:
    25  	// found 6 at index 2 in [1 3 6 10 15 21 28 36 45 55]
    26  }
    27  
    28  // This example demonstrates searching a list sorted in descending order.
    29  // The approach is the same as searching a list in ascending order,
    30  // but with the condition inverted.
    31  func ExampleSearch_descendingOrder() {
    32  	a := []int{55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
    33  	x := 6
    34  
    35  	i := sort.Search(len(a), func(i int) bool { return a[i] <= x })
    36  	if i < len(a) && a[i] == x {
    37  		fmt.Printf("found %d at index %d in %v\n", x, i, a)
    38  	} else {
    39  		fmt.Printf("%d not found in %v\n", x, a)
    40  	}
    41  	// Output:
    42  	// found 6 at index 7 in [55 45 36 28 21 15 10 6 3 1]
    43  }
    44  
    45  // This example demonstrates finding a string in a list sorted in ascending order.
    46  func ExampleFind() {
    47  	a := []string{"apple", "banana", "lemon", "mango", "pear", "strawberry"}
    48  
    49  	for _, x := range []string{"banana", "orange"} {
    50  		i, found := sort.Find(len(a), func(i int) int {
    51  			return strings.Compare(x, a[i])
    52  		})
    53  		if found {
    54  			fmt.Printf("found %s at index %d\n", x, i)
    55  		} else {
    56  			fmt.Printf("%s not found, would insert at %d\n", x, i)
    57  		}
    58  	}
    59  
    60  	// Output:
    61  	// found banana at index 1
    62  	// orange not found, would insert at 4
    63  }
    64  
    65  // This example demonstrates searching for float64 in a list sorted in ascending order.
    66  func ExampleSearchFloat64s() {
    67  	a := []float64{1.0, 2.0, 3.3, 4.6, 6.1, 7.2, 8.0}
    68  
    69  	x := 2.0
    70  	i := sort.SearchFloat64s(a, x)
    71  	fmt.Printf("found %g at index %d in %v\n", x, i, a)
    72  
    73  	x = 0.5
    74  	i = sort.SearchFloat64s(a, x)
    75  	fmt.Printf("%g not found, can be inserted at index %d in %v\n", x, i, a)
    76  	// Output:
    77  	// found 2 at index 1 in [1 2 3.3 4.6 6.1 7.2 8]
    78  	// 0.5 not found, can be inserted at index 0 in [1 2 3.3 4.6 6.1 7.2 8]
    79  }
    80  
    81  // This example demonstrates searching for int in a list sorted in ascending order.
    82  func ExampleSearchInts() {
    83  	a := []int{1, 2, 3, 4, 6, 7, 8}
    84  
    85  	x := 2
    86  	i := sort.SearchInts(a, x)
    87  	fmt.Printf("found %d at index %d in %v\n", x, i, a)
    88  
    89  	x = 5
    90  	i = sort.SearchInts(a, x)
    91  	fmt.Printf("%d not found, can be inserted at index %d in %v\n", x, i, a)
    92  	// Output:
    93  	// found 2 at index 1 in [1 2 3 4 6 7 8]
    94  	// 5 not found, can be inserted at index 4 in [1 2 3 4 6 7 8]
    95  }
    96  

View as plain text