When you want to find something in a list, it’s important to know which method is faster—especially when the list gets big. Two common methods for searching are Linear Search and Binary Search. They both help you find items, but they work in different ways, which affects how fast they are.
Linear search is the most straightforward way to look for something.
Let's say you have a phone book, and you need to find your friend's phone number. You would probably go through the pages one at a time until you find the right number. Here’s how linear search works:
Time Taken: If you have a list with items and you need to look at every single one, it could take up to checks. This means the time taken is .
Binary search is much quicker, but it only works on sorted lists. Let’s go back to the phone book example. Instead of checking every page, you would open it to the middle. You would check that number and decide if you need to go forward or backward based on whether your friend’s number is higher or lower. Here’s how it works:
Time Taken: This method cuts down the number of checks needed. Each time you check, you reduce the number of items you’re looking at by half. So the time taken is .
Let’s look at an example of searching for a name in a phone book with 1,000 pages:
As the lists get bigger, the speed of binary search makes a huge difference! In short, if you have a sorted list, binary search is much faster than linear search. That’s why it’s often the better choice in many situations.
When you want to find something in a list, it’s important to know which method is faster—especially when the list gets big. Two common methods for searching are Linear Search and Binary Search. They both help you find items, but they work in different ways, which affects how fast they are.
Linear search is the most straightforward way to look for something.
Let's say you have a phone book, and you need to find your friend's phone number. You would probably go through the pages one at a time until you find the right number. Here’s how linear search works:
Time Taken: If you have a list with items and you need to look at every single one, it could take up to checks. This means the time taken is .
Binary search is much quicker, but it only works on sorted lists. Let’s go back to the phone book example. Instead of checking every page, you would open it to the middle. You would check that number and decide if you need to go forward or backward based on whether your friend’s number is higher or lower. Here’s how it works:
Time Taken: This method cuts down the number of checks needed. Each time you check, you reduce the number of items you’re looking at by half. So the time taken is .
Let’s look at an example of searching for a name in a phone book with 1,000 pages:
As the lists get bigger, the speed of binary search makes a huge difference! In short, if you have a sorted list, binary search is much faster than linear search. That’s why it’s often the better choice in many situations.