Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 33 / 68

The binary search algorithm

This lesson covers implementing binary search in Julia, with the aim of understanding how to find a value efficiently.

Binary search finds a value efficiently in already-sorted data. Picture opening a dictionary in the middle and halving the range each time.

The sample code calculates the middle mid from the two positions low and high, compares it with the target, and narrows the range by updating low or high. The รท integer-division operator is a characteristic Julia touch.

A common early stumble is that binary search requires the array to be sorted beforehand. That is a precondition, so do not forget to sort first.

In professional work, the idea behind binary search is applied widely wherever a particular value has to be found quickly in a large body of data.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)