Linear Search
Last updated
Last updated
Searching for items in an array is an important and common task in computer science. For example, a meterologist may want to konw the hottest day on record in a given month. Teachers may want to find a particular student in a class roster. A skateboard shop may want to check their inventory to see if they have a certain deck in stock. Storing data in a computer allows information like this to be easily searched and retrieved.
One way to search through a list of items is to start at the beginning of the list and continue through the list until the desired item is found. If the desired item is not found, then that means it is not in the list.
Suppose that you are given a set of raffle tickets at a school raffle. Each ticket has its own number on it. Your tickets may look like this:
The 0th ticket has number 44, and the ticket at index 4 has raffle number 64. The school will randomly select a number between 1 and 100. If you have the ticket with that number, you win!
What if the school picks 51 as the winning number? As a human, it's easy to look over the whole list and see that you have the ticket with 51 on it. But computers can't look over a whole list the way a human can. Computers need a more programmatic approach to looking through the list.
Linear search isn't too difficult to implement. To programmatically searching our list of raffle ticket numbers, we just need to iterate through the list one number at a time. At each new number, if the number matches the desired number, we know that the desirred number is in the list, so we can stop searching and return the index position of the number. If we make it all the way to the end of the list and haven't spotted our desired number, then that means it's not in the list.
Here's what it looks like in pseudocode:
We can then turn that pseudocode into real Java code:
Ticket
0
1
2
3
4
5
Number
44
53
12
51
64
15