Unit 2 Computational Thinking Algorithms Programming Search Algorithms

  • Slides: 12
Download presentation
Unit 2: Computational Thinking, Algorithms & Programming Search Algorithms 2. 1 - Algorithms

Unit 2: Computational Thinking, Algorithms & Programming Search Algorithms 2. 1 - Algorithms

Unit 2: Computational Thinking, Algorithms & Programming 26 May 2021 Search Algorithms Learning Objective:

Unit 2: Computational Thinking, Algorithms & Programming 26 May 2021 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: 1. I can list the steps in a binary search algorithm. 2. I can use the binary search algorithm to search for items in an ordered list. 3. I can list the steps in a linear search algorithm. 4. I can use the linear search algorithm to search for items in an unordered list.

Unit 2: Computational Thinking, Algorithms & Programming Search Algorithms When searching for information within

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)

Unit 2: Computational Thinking, Algorithms & Programming Binary Search A binary search looks for

Unit 2: Computational Thinking, Algorithms & Programming Binary Search A binary search looks for items in an ordered list. To find the middle item Steps in a binary search are: in a list of n items do 1. Put the list into order (if it is not already) (n+1) ÷ 2 and round up if necessary. 2. Find the middle item in the ordered list. 3. If this is the item you’re looking for, then stop the search – you’ve found it. 4. 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. 5. 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.

Unit 2: Computational Thinking, Algorithms & Programming Binary Search Example: We are going to

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 = 5 th item. The 5 th item is 68 and 68 <99 so get rid of the first half of the list to leave: 92 94 99 133 There are 4 items left so the middle item is the (4+1) ÷ 2 = 2. 5 = 3 rd item. The 3 rd item is 99. You’ve found the item you’re looking for so the search is complete. 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!

Unit 2: Computational Thinking, Algorithms & Programming Task: Binary Search Use the binary search

Unit 2: Computational Thinking, Algorithms & Programming Task: Binary Search Use the binary search algorithm to: 1. Find the number 23 from the list. 2 15 20 23 30 34 45 50 51 2. Find the number 145 from the list. 25 45 60 85 100 115 135 145 160

Unit 2: Computational Thinking, Algorithms & Programming Linear Search A linear search can be

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.

Unit 2: Computational Thinking, Algorithms & Programming Linear Search: The Steps 1. Look at

Unit 2: Computational Thinking, Algorithms & Programming Linear Search: The Steps 1. Look at the first item in the unordered list. 2. If this is the item you’re looking for, then stop the search – you’ve found it! 3. If not, then look at the next item in the list. 4. 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….

Unit 2: Computational Thinking, Algorithms & Programming Linear Search: Example Use the linear search

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 Check the first item: 7 ≠ 99 Look at the next item: 21 ≠ 99 Look at the next item: 52 ≠ 99 Look at the next item: 59 ≠ 99 Look at the next item: 68 ≠ 99 Look at the next item: 92 ≠ 99 Look at the next item: 94 ≠ 99 Look at the next item: 99 = 99 92 94 99 133 You’ve found the item you’re looking for so the search is complete. ≠ means NOT

Unit 2: Computational Thinking, Algorithms & Programming Task: Linear Search Use the linear search

Unit 2: Computational Thinking, Algorithms & Programming Task: Linear Search Use the linear search algorithm to: 1. Find the number 23 from the list. 2 15 20 23 30 34 45 50 51 2. Find the number 145 from the list. 25 45 60 85 100 115 135 145 160

Unit 2: Computational Thinking, Algorithms & Programming Exam Style Questions: Linear Describe an algorithm

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]

Unit 2: Computational Thinking, Algorithms & Programming Exam Style Questions: Binary Describe the stages

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]