Skip to content
← Back to glossary

2分探索法Binary search

Learn in the textbookSearch and Sorting Algorithms

DefinitionBinary search (2分探索法) works on sorted data, in increasing (ascending) or decreasing (descending) order, and compares the target with the value in the middle of the range, keeping only the half where the target must be.

ExplanationWith ascending data, you keep only the first half when the target is smaller than the middle value and only the second half when it is larger, repeating until you find it or the range is empty. Each comparison removes about half of the candidates, so doubling the number of items adds only about one comparison. The count still depends on where the target sits, though, and a value near the start can be found in fewer comparisons by linear search.

AnalogyIt is like pulling the middle slip from a stack of numbered tickets, deciding whether your number comes before or after it, and then checking the middle of the half that remains.

Memory hookThe "binary" in binary search means "two": every step cuts the range in two.

Related past-exam questions(1 question)