Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1: Problem Solving

Similar presentations


Presentation on theme: "Topic 1: Problem Solving"— Presentation transcript:

1 Topic 1: Problem Solving
Searching Algorithms

2 Problem Solving: Searching Algorithms
We’ve seen two important Sorting Algorithms Which take a collection of values And order them (typically from least to greatest) These were the Bubble Sort and the Merge Sort This time we’re going to look at searching algorithms These types of algorithms solve one particular problem Is a specific value in this collection? Problem Solving: Searching Algorithms

3 Problem Solving: Searching Algorithms
There may come a time when we need to search for a particular value It doesn’t have to be a number, though that is the common example We could be searching for A person’s name in a register A scheduled event on a calendar An ID in a table of a database Problem Solving: Searching Algorithms

4 Searching Algorithms We are going to look at two specific algorithms in this topic The Linear Search runs through the collection in a simple, one-way direction The Binary Search removes half of the collection as needed to ‘filter down’ to a single value Although the Linear Search is simpler, the Binary Search takes less time to complete Click here for a good online visualiser for both search algorithms Problem Solving: Searching Algorithms

5 Problem Solving: Searching Algorithms
Linear Search The Linear Search algorithm itself is simple, considering what we’ve looked at so far All it’s doing is, starting at the beginning of the collection, looking at each element one-by- one If it finds the element we’re looking for, we’re done Otherwise we move on to the next element If we finish looking through the whole collection, then the value we’re looking for is not in the collection BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

6 Problem Solving: Searching Algorithms
Linear Search One important note to make about this algorithm We can either make it return True or False OR the index of the found value A simple yes/no answer is handled by the first option If we want to do something with the found value, then the second option would be needed BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

7 Problem Solving: Searching Algorithms
Linear Search At the moment this algorithm returns a yes or no value We can easily change it to return an index instead Can you guess how we do this? BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

8 Problem Solving: Searching Algorithms
Linear Search We simply use a regular for-loop instead Which means creating a counter for the index We can then return that index If the current value is what we’re looking for If we get past all the values, we know the value isn’t there So we can return -1 As -1 is not a valid index BEGIN LinearSearch(nums, search) FOR index FROM 0 TO SIZE(nums) - 1 IF nums[index] = search RETURN index END IF END FOR RETURN -1 END LinearSearch Problem Solving: Searching Algorithms

9 Problem Solving: Searching Algorithms
Implement the Linear Search algorithm in your programming language of choice Create two versions of it One returning true/false The other returning the index of the item Don’t forget, return -1 if it wasn’t found BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

10 Problem Solving: Searching Algorithms
Binary Search The Binary Search algorithm works a little bit differently Rather than going from the beginning to the end of the collection (like Linear Search) It starts in the middle It then compares the middle against the value we’re looking for If the middle is too big, we remove the upper half of the collection If the middle is too small, we remove the lower half of the collection Either way, we remove half the values and repeat Problem Solving: Searching Algorithms

11 Problem Solving: Searching Algorithms
Binary Search Here is the pseudocode for this algorithm We start by making values for the lower and upper limits These are indexes They will move as we ‘remove’ half the values From these, we can calculate the mid The value we’ll compare the search against BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

12 Problem Solving: Searching Algorithms
Binary Search Once we’ve got the mid, we compare it against the search If it is too big, we move the upper down If it is too small, we move the lower up If the mid equals the search, then we’ve found the value If the lower is ever larger than the upper, we know we’ve run out of values to look at Meaning the search isn’t in the collection BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

13 Problem Solving: Searching Algorithms
Binary Search The most important thing to remember about this algorithm is… It REQUIRES the values to be IN ORDER That means the collection need to be sorted before we can use Binary Search BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

14 Problem Solving: Searching Algorithms
Implement the Binary Search algorithm Remember to sort the array first Make two versions One returns true/false The other returns the index BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

15


Download ppt "Topic 1: Problem Solving"

Similar presentations


Ads by Google