0Pricing
Go Academy · Lesson

Searching Sorted Data

Binary search.

Searching Sorted Data is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Search Sorted Data

Once a slice is sorted, you can find elements far faster using binary search instead of scanning every item.

Go's sort package provides search helpers that run in logarithmic time.

Linear vs Binary

A linear search checks each element one by one (O(n)). Binary search halves the search range each step (O(log n)), but requires the data to be sorted first.

sort.SearchInts

sort.SearchInts finds the index where a value is, or where it would be inserted to keep the slice sorted. The slice must already be sorted ascending.

package main

import (
	"fmt"
	"sort"
)

func main() {
	nums := []int{1, 3, 5, 7, 9}
	i := sort.SearchInts(nums, 5)
	fmt.Println("index:", i)
}

Confirming a Match

SearchInts returns an index even if the value is absent (the insertion point). Always confirm by checking i < len(s) && s[i] == target.

package main

import (
	"fmt"
	"sort"
)

func main() {
	nums := []int{1, 3, 5, 7}
	target := 4
	i := sort.SearchInts(nums, target)
	found := i < len(nums) && nums[i] == target
	fmt.Println("index:", i, "found:", found)
}

Insertion Point

When a value is missing, the returned index is exactly where you would insert it to keep things sorted. Here 4 would go at index 2.

package main

import (
	"fmt"
	"sort"
)

func main() {
	nums := []int{1, 3, 5, 7}
	i := sort.SearchInts(nums, 4)
	fmt.Println("insert 4 at index:", i)
}

SearchStrings

sort.SearchStrings does the same for a sorted string slice.

package main

import (
	"fmt"
	"sort"
)

func main() {
	words := []string{"apple", "cherry", "mango"}
	i := sort.SearchStrings(words, "cherry")
	fmt.Println("index:", i)
}

The General sort.Search

sort.Search is the flexible core. You give it a length and a function f that is false then true; it returns the smallest index where f is true.

package main

import (
	"fmt"
	"sort"
)

func main() {
	nums := []int{2, 4, 6, 8, 10}
	i := sort.Search(len(nums), func(i int) bool {
		return nums[i] >= 6
	})
	fmt.Println("first >= 6 at index:", i)
}

Find First Element Above

Because sort.Search finds the boundary where the condition flips to true, it is great for queries like first value greater than a threshold.

package main

import (
	"fmt"
	"sort"
)

func main() {
	scores := []int{10, 20, 30, 40}
	i := sort.Search(len(scores), func(i int) bool {
		return scores[i] > 25
	})
	fmt.Println("first > 25:", scores[i])
}

Must Be Sorted First

Binary search assumes order. If the slice is not sorted, the results are meaningless. Always sort before searching.

package main

import (
	"fmt"
	"sort"
)

func main() {
	nums := []int{9, 1, 5, 3}
	sort.Ints(nums)
	i := sort.SearchInts(nums, 5)
	fmt.Println(nums, "-> index of 5:", i)
}

Performance Win

For a slice of one million elements, linear search may check a million items; binary search checks about 20. The cost of sorting once pays off across many searches.

Choosing a Search Helper

Summary:

  • sort.SearchInts / SearchStrings / SearchFloat64s - typed slices
  • sort.Search - custom condition on any indexable data

Quick Check

You call sort.SearchInts(s, 4) on a sorted slice that does not contain 4. What does it return?

Recap

Searching sorted data with binary search:

  • Data must be sorted first
  • Helpers return an index or insertion point
  • Confirm matches with an equality check
  • sort.Search handles custom conditions

Frequently asked questions

Is the “Searching Sorted Data” lesson free?

Yes — the full text of “Searching Sorted Data” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Searching Sorted Data”?

Binary search. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Searching Sorted Data” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Sorting Slices
  2. Custom Sort Orders
  3. Searching Sorted Data
  4. Stable Sorting
← Back to Go Academy