Golang sort.SearchInts() Function with Examples

Golang | sort.SearchInts() Function: Here, we are going to learn about the SearchInts() function of the sort package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 11, 2021

sort.SearchInts()

The SearchInts() function is an inbuilt function of the sort package which is used to search for the given element (x) in a sorted slice of ints and returns the index as specified by Search().

It accepts two parameters (a []int, x int) – a is the sorted slice of int type and x is an int type element to search and returns the index as specified by Search().

Note: The SearchInts() result is the index to insert the elements (x), if x is not present (it could be len(a)). The slice must be sorted in ascending order.

Syntax:

func SearchInts(a []int, x int) int

Parameter(s):

  • a : A sorted slice of ints.
  • x : An int type elements to be searched within a.

Return Value:

The return type of the SearchInts() function is an int, it returns the index as specified by Search.

Example 1:

// Golang program to demonstrate the
// example of sort.SearchInts() Function

package main

import (
	"fmt"
	"sort"
)

func main() {
	a := []int{10, 20, 25, 27, 30}

	x := 25
	fmt.Println(sort.SearchInts(a, x))

	x = 30
	fmt.Println(sort.SearchInts(a, x))

	x = 10
	fmt.Println(sort.SearchInts(a, x))

	// items not found case
	x = 5
	fmt.Println(sort.SearchInts(a, x))

	x = 35
	fmt.Println(sort.SearchInts(a, x))
}

Output:

2
4
0
0
5

Example 2:

// Golang program to demonstrate the
// example of sort.SearchInts() Function

package main

import (
	"fmt"
	"sort"
)

func main() {
	a := []int{10, 20, 25, 27, 30}

	x := 25
	i := sort.SearchInts(a, x)
	fmt.Printf("Element %d found at index %d in %v\n", x, i, a)

	x = 5
	i = sort.SearchInts(a, x)
	fmt.Printf("Element %d not found, it can inserted at index %d in %v\n", x, i, a)

	x = 40
	i = sort.SearchInts(a, x)
	fmt.Printf("Element %d not found, it can inserted at index %d in %v\n", x, i, a)
}

Output:

Element 25 found at index 2 in [10 20 25 27 30]
Element 5 not found, it can inserted at index 0 in [10 20 25 27 30]
Element 40 not found, it can inserted at index 5 in [10 20 25 27 30]

Example 3:

// Golang program to demonstrate the
// example of sort.SearchInts() Function

package main

import (
	"fmt"
	"sort"
)

// Main function
func main() {
	a := []int{10, 10, 5, 20, 25}

	b := []int{50, 10, 40, 30, 20}

	var x1, x2 int
	x1 = 20
	x2 = 30

	// Sorting  both slices of ints
	sort.Ints(a)
	sort.Ints(b)

	// Printing both slices of ints
	fmt.Println("a: ", a)
	fmt.Println("b: ", b)

	// Searching elements
	ind1 := sort.SearchInts(a, x1)
	ind2 := sort.SearchInts(b, x2)

	// Displaying the results
	fmt.Println("Result 1: ", ind1)
	fmt.Println("Result 2: ", ind2)
}

Output:

a:  [5 10 10 20 25]
b:  [10 20 30 40 50]
Result 1:  3
Result 2:  2

Golang sort Package »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.