- Start at the beginning of the list.
- Compare each item in the list with the search item.
- Stop when the item is found or the end of the list is reached.
- Simple to code.
- Works with an unsorted list.
- Slow, especially with large lists.
Binary Search:
- Find the middle item of the Current search list.
- Check if the Current Item is the search item.
- If not then make the Current Search List either the Higher Half or the Lower Half of the list.
- Repeat from Step 1
- Much Faster than a Linear Search, especially as the list increases in size.
e.g.
Disadvantages:
- The list needed to be sorted for the binary search to take place.
- It's more complex to code.
No comments:
Post a Comment