Standard Searching Algorithms
No description yet. This curated deck is ready for daily study.
Card preview (8)
What happens in a binary search?
• In a binary search, we locate the midpoint of the data set, removing irrelevant halves of our data until we find our piece of data.
Example of a binary search - Find the number 26 in the data set (5, 10, 26, 56, 68, 75, 96)
• write under each number its index (first one is 0) • work out the midpoint by adding together the first index in the list and the last index in the list. We then divide that answer by 2 • remove the side that the value isn’t on (all the values to the right of 56 (the midpoint) as we know 56 is bigger than 26) • find the midpoint of the new list and round down if it’s a decimal. -index 1 = 10 so get rid of values left of 10 and find the new midpoint • = 2. Index 2 = 26. 3 searches
What happens in a linear search?
• We go down the list checking each piece of data in turn until we find the data we’re looking for. • If we check a piece of data and it is not the one we’re looking for, we move onto the next one. • The data doesn’t need to be in order.
Example of a linear search - find 26 in: 75, 10, 26, 97, 68, 5, 56
• First piece = 75 • Second piece = 10 • Third piece = 26 • 26 is what we’re looking for. Three searches.
Advantages of a binary search -
• Better for searching through large data sets as it is quicker in locating data (reduces size of list) • It’s a fairly simple and straightforward searching algorithm to use
Disadvantages of a binary search -
• Not effective for searching through smaller data sets as it overcomplicates the problem and therefore would take longer. • Only works on sorted lists.
Advantages of a linear search -
• Can be used on any list, whether it’s sorted or unsorted • Better for searching through small data sets
Disadvantages of a linear search -
• Ineffective when used on a large data set, as checking each piece of data on large data sets can be time consuming.