Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 15: Processing Arrays

Similar presentations


Presentation on theme: "Lesson 15: Processing Arrays"— Presentation transcript:

1 Lesson 15: Processing Arrays
UNIT 5 – LESSON 15 – WED, JANUARY 17, 2018

2 VOCABULARY ALERT: for loop - A typical looping construct designed to make it easy to repeat a section of code using a counter variable. The for loop combines the creation of a variable, a Boolean looping condition, and an update to the variable in one statement.

3 NEW CODE

4 PURPOSE: Use a for loop in a program to implement an algorithm that processes all elements of an array. Write code that implements a linear search on an unsorted array of numbers. Write code to find the minimum value in an unsorted list of numbers. Explain how binary search is more efficient than linear search but can only be used on sorted lists.

5 This lesson introduces students to algorithms that process lists of data. The for loop is re-introduced to implement these algorithms because it’s straightforward to use to process all the elements of a list. 

6 The for loop consolidates all of those pieces - counter variable, incrementing, and boolean condition - onto one line. One of the most common uses of for loops in programming is to process arrays.

7 for loops allow programmers to easily step through all the elements in an array.

8 This basic pattern is at the core of many algorithms used to process a list of items. Whether you are looking to find a name in a list, find the closest store to your current location, or compute the total money in your account based on past transactions, a loop will probably be used at some point to access all the elements in a list.

9 Remember in a lesson a while back when we wrote algorithms for playing cards using the "Human Machine Language"? Notice how a row of cards is kind of like a list.

10 Today we’re going to begin to write code to process lists of data
Today we’re going to begin to write code to process lists of data. Processing large lists of data is one of the most powerful things computer programs can do. Many of the most important algorithms in computer science have their roots in processing lists of data.

11 Get into pairs Get some cards Distribute: Minimum Card Algorithm - Activity Guide

12 The same kind of thinking that went into designing this algorithms can be applied to making working code as well. Don't confuse thinking about the algorithm with actually writing code. Today you'll get some practice writing code with loops and if-statements to process a list - skills that will help you write your own algorithms for lists.

13 Get into Code Studio Work through the lessons

14 PART 2 – DO THIS AFTER YOU COMPLETE CODE STUDIO
PART 2 – DO THIS AFTER YOU COMPLETE CODE STUDIO! In the lesson you programmed linear search (scan all the values in the list from beginning to end until you find what you’re looking for). “Binary search” uses a different algorithm, that is faster, but requires that the list be in sorted order ahead of time - linear search will work for any list. This algorithm can only be performed on sorted arrays and justify the fact that it is faster. Distribute: Card Search Algorithm - Activity Guide

15 When you talk about how “long” or how much “time” an algorithm takes to run, time is usually a measure of the number of operations a computer needs to perform to complete the task. You can measure the amount of time it takes to run an algorithm on a clock, but it’s often not a useful measure, because the speed of the computer hardware obscures whether the algorithm is good or not.

16 PRACTICE AP TEST QUESTIONS: These statements are taken directly from the CSP Framework under “4.2.4 Evaluate algorithms analytically and empirically for efficiency, correctness, and clarity. [P4]” 4.2.4D Different correct algorithms for the same problem can have different efficiencies. Both linear search and binary search solve the same problem, but they have different efficiencies. 4.2.4E Sometimes more efficient algorithms are more complex. Binary search is more efficient than linear search, but even though it might be easy to understand at a high level, it is much more challenging to write code for. 4.2.4F Finding an efficient algorithm for a problem can help solve larger instances of the problem. The algorithms we wrote work for any size input. 4.2.4G Efficiency includes both execution time and memory usage. Execution “time” here means number of operations that need to be performed in the worst case. 4.2.4H Linear search can be used when searching for an item in any list; binary search can be used only when the list is sorted. Emphasis should be placed on the fact that binary search only works when the list is sorted. It’s a fact often forgotten.

17 To turn in for Unit 5 – Lesson 15:
AG – Minimum Card Algorithm AG – Card Searching Algorithm U5L15 - #5 Update values in an Array U5L15 - #10 Print True/False U5L15 - #11 Flag U5L15 - #14 General Search U5L15 - #15 Reusing a Function Pattern


Download ppt "Lesson 15: Processing Arrays"

Similar presentations


Ads by Google