Golang sort.SearchFloat64s() Function with Examples

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

sort.SearchFloat64s()

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

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

Note: The SearchFloat64s() 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 SearchFloat64s(a []float64, x float64) int

Parameter(s):

  • a : A sorted slice of float64s.
  • x : A float64 type elements to be searched within a.

Return Value:

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

Example 1:

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

package main

import (
	"fmt"
	"sort"
)

func main() {
	a := []float64{10.50, 20.0, 25.60, 27.50, 30.0}

	x := 25.60
	fmt.Println(sort.SearchFloat64s(a, x))

	x = 30.0
	fmt.Println(sort.SearchFloat64s(a, x))

	x = 10.5
	fmt.Println(sort.SearchFloat64s(a, x))

	// items not found case
	x = 10.0
	fmt.Println(sort.SearchFloat64s(a, x))

	x = 35.50
	fmt.Println(sort.SearchFloat64s(a, x))
}

Output:

2
4
0
0
5

Example 2:

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

package main

import (
	"fmt"
	"sort"
)

func main() {
	a := []float64{10.50, 20.0, 25.60, 27.50, 30.0}

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

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

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

Output:

Element 25.6 found at index 2 in [10.5 20 25.6 27.5 30]
Element 10 not found, it can inserted at index 0 in [10.5 20 25.6 27.5 30]
Element 40.5 not found, it can inserted at index 5 in [10.5 20 25.6 27.5 30]

Example 3:

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

package main

import (
	"fmt"
	"sort"
)

// Main function
func main() {
	a := []float64{10.50, 10.0, 5.15, 20.50, 25.12}

	b := []float64{50.50, 10.10, 40.40, 30.30, 20.20}

	var x1, x2 float64
	x1 = 20.50
	x2 = 30.30

	// Sorting  both slices of float64s
	sort.Float64s(a)
	sort.Float64s(b)

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

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

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

Output:

a:  [5.15 10 10.5 20.5 25.12]
b:  [10.1 20.2 30.3 40.4 50.5]
Result 1:  3
Result 2:  2

Golang sort Package »




Comments and Discussions!

Load comments ↻





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