Download presentation
Presentation is loading. Please wait.
Published byAnissa Price Modified over 5 years ago
1
Unit 2: Computational Thinking, Algorithms & Programming
Search Algorithms 2.1 - Algorithms
2
Saturday, 18 May 2019 Search Algorithms
Unit 2: Computational Thinking, Algorithms & Programming Saturday, 18 May 2019 Search Algorithms Learning Objective: To be able to demonstrate an understanding of the algorithms that are used to search for information in large amounts of data. Success Criteria: I can list the steps in a binary search algorithm. I can use the binary search algorithm to search for items in an ordered list. I can list the steps in a linear search algorithm. I can use the linear search algorithm to search for items in an unordered list.
3
Unit 2: Computational Thinking, Algorithms & Programming
Search Algorithms When searching for information within large amounts of data, a computer uses algorithms in order to do this. There are 2 simple methods that you need to be aware of: Binary Search Algorithm (ordered lists) Linear Search Algorithm (unordered lists)
4
Unit 2: Computational Thinking, Algorithms & Programming
Binary Search A binary search looks for items in an ordered list. Steps in a binary search are: Put the list into order (if it is not already) Find the middle item in the ordered list. If this is the item you’re looking for, then stop the search – you’ve found it. If not, compare the item you’re looking for to the middle item. If it comes before the middle item, get rid of the second half of the list. If it comes after the middle item, get rid of the first half of the list. You’ll be left with a list that is half the size of the original list. Repeat steps 1-3 on this smaller list to get an even smaller one. Keep going until you find the item you’re looking for. To find the middle item in a list of n items do (n+1) ÷2 and round up if necessary.
5
Binary Search Example:
Unit 2: Computational Thinking, Algorithms & Programming Binary Search Example: We are going to use the binary search algorithm to find the number 99 in the following list: 7 21 52 59 68 92 94 99 133 There are 9 items in the list so the middle item is the (9+1) ÷ 2 = 5th item. The 5th item is 68 and 68 <99 so get rid of the first half of the list to leave: There are 4 items left so the middle item is the (4+1) ÷ 2 = 2.5 = 3rd item. The 3rd item is 99. You’ve found the item you’re looking for so the search is complete. 92 94 99 133 Exam Tip: You will need to show the steps in a question like this. You are likely to get asked a 4-6 mark question on one of these!
6
Unit 2: Computational Thinking, Algorithms & Programming
Task: Binary Search Use the binary search algorithm to: Find the number 23 from the list. 2. Find the number 145 from the list. 2 15 20 23 30 34 45 50 51 25 45 60 85 100 115 135 145 160
7
Unit 2: Computational Thinking, Algorithms & Programming
Linear Search A linear search can be used on an Unordered list. A linear search checks each item of the list in turn to see if it’s the correct one, it stops when it either finds the item it’s looking for or has checked every item. A linear search is much simpler than a binary search but less efficient. A linear search can be used on any type of list, it doesn’t have to be ordered. Due to it being inefficient, it is often only used on small lists.
8
Linear Search: The Steps
Unit 2: Computational Thinking, Algorithms & Programming Linear Search: The Steps Look at the first item in the unordered list. If this is the item you’re looking for, then stop the search – you’ve found it! If not, then look at the next item in the list. Repeat steps 2 and 3 until you find the item that you’re looking for or you’ve checked every item. Lets look at an example….
9
Linear Search: Example
Unit 2: Computational Thinking, Algorithms & Programming Linear Search: Example Use the linear search to find the number 99 from the previous list… 7 21 52 59 68 92 94 99 133 Check the first item: 7 ≠ 99 Look at the next item: 21 ≠ 99 52 ≠ 99 59 ≠ 99 68 ≠ 99 92 ≠ 99 94 ≠ 99 99 = 99 ≠ means NOT You’ve found the item you’re looking for so the search is complete.
10
Unit 2: Computational Thinking, Algorithms & Programming
Task: Linear Search Use the linear search algorithm to: Find the number 23 from the list. 2. Find the number 145 from the list. 2 15 20 23 30 34 45 50 51 25 45 60 85 100 115 135 145 160
11
Exam Style Questions: Linear
Unit 2: Computational Thinking, Algorithms & Programming Exam Style Questions: Linear Describe an algorithm expressed in pseudocode to ask a user for a search item and carry out a linear search on data stored in an array to find that item. [6 marks]
12
Exam Style Questions: Binary
Unit 2: Computational Thinking, Algorithms & Programming Exam Style Questions: Binary Describe the stages in applying a binary search to the following list to find the number 17. 3, 5, 9, 14, 17, 21, 27, 31, 35, 37, 39, 40, 42. [4 marks]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.