Binary Search

Here’s a simple example to understand what binary search in computer science refers to. Let’s say, you had an array of numbers from 1 to 100. You have to choose one of them based on some given criteria and report your answer. You computer tells you whether your entry is too high, too low or correct.

To find the right answer, you can test number 50. If the computer tells you that your entry was too high, you know that the number should be between 1 and 50 but not including 50. So you have practically eliminated half the numbers in one run. Next you could enter 25 for instance which is some number around (1 + 50) /2 and see if your entry was too high, too low or the correct answer and by the same principle, you can eliminate half of the remaining numbers.

And running the test for a few times, you can get to the right answer. This is called binary search in computer science and is used in computer programs and algorithms very widely.

Linear Search


Here’s a simple situation to understand what linear search in computer science refers to. Let’s say you had an array of numbers, say 1 to 100, and were to guess or find the right one in a given situation. Only one of the numbers is the right choice based on some given criteria.

There are different ways to think about finding the answer but one approach would be that you tested each number starting from 1, tested it against the given criteria. If it’s the right answer, stop the search and report the answer, if not, go to the next number and do the test again until you get to the right one.

This approach in computer science is called linear search. Using linear search in our example, numbers from 1 to 100, you could need 100 tests to find the right answer if the right answer were number 100.