Practicing bisect in Go
In Python, we are used to using bisect
to search for elements in the List. In Go, sort.Search()
can also provide us with the same purpose.
As in Python, the target array is sorted before searching.
|
|
After sorting, we use sort.Search(), the first parameter is the search target, and the second is the function of the search condition. Here we input slice: [1 2 3 4 5 5 6 9], find the 5 closest to the left, and the program returns the index 4.