Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS 001 4.1 Today’s topics l Algorithms and pseudocode l Notes adapted from Marti Hearst at UC Berkeley and David Smith at Georgia Tech Upcoming ä Beginning.

Similar presentations


Presentation on theme: "CPS 001 4.1 Today’s topics l Algorithms and pseudocode l Notes adapted from Marti Hearst at UC Berkeley and David Smith at Georgia Tech Upcoming ä Beginning."— Presentation transcript:

1 CPS 001 4.1 Today’s topics l Algorithms and pseudocode l Notes adapted from Marti Hearst at UC Berkeley and David Smith at Georgia Tech Upcoming ä Beginning Java Reading ä “Cyberspace as a Human Right”, William McIver ä Back of the Envelope Calculations ä Chapter 2 in Great Ideas

2 CPS 001 4.2 Central role of algorithms in CS

3 CPS 001 4.3 What’s wrong with this algorithm? (From back of shampoo bottle) Directions: Wet Hair Apply a small amount of shampoo Lather Rinse Repeat

4 CPS 001 4.4 Properties of good algorithms l Good algorithms must be ä Correct ä Complete ä Precise ä Unambiguous l And should be ä Efficient ä Simple ä Contain levels of abstraction

5 CPS 001 4.5 Algorithms l Hand-waving not allowed! l Specifying algorithms requires you to say what is really involved in making it work. l Example: ä How does a computer work? ä Hand-wave: zeros & ones ä Real answer: see later part of class. l You learn to know when you don’t know ä “I know nothing except the fact of my ignorance.” ä Socrates, from Diogenes Laertius, Lives of Eminent Philosophers

6 CPS 001 4.6 Describing Algorithms Pictures Natural language (English) Pseudo-code Specific high-level programming language More easily expressed More precise

7 CPS 001 4.7 Pseudocode l A shorthand for specifying algorithms l Leaves out the implementation details l Leaves in the essence of the algorithm l What does this algorithm do? l How many times does it print Hello? <

8 CPS 001 4.8 Sequential search

9 CPS 001 4.9 Picking courses 1.Make a list of courses you want to register for, in order of priority 2.Start with empty schedule. Number of courses = 0. 3.Choose highest priority class on list. 4.If the chosen class is not full and its class time does not conflict with classes already scheduled, then register for the class (2 steps): 1.Add the class to the schedule 2.Increment the number of classes scheduled 5.Cross that class off of your list. 6.Repeat steps 3 through 5 until the number of classes scheduled is >= 4, or until all classes have been crossed out. 7.Stop.

10 CPS 001 4.10 Flowcharts End Begin Make list of classes you want to take Num Classes = 0 Choose highest priority class on list Add the class to your schedule. Increment Num Classes. Cross the class off your list. yes no Is this class full? Is there a time conflict? Num Classes >= 4? More classes on list? yes no

11 CPS 001 4.11 Origami as Algorithm From Brookshear; Copyright 2003 Pearson Education

12 CPS 001 4.12 From Brookshear; Copyright 2003 Pearson Education Origami as Algorithm

13 CPS 001 4.13 Origami primitives From Brookshear; Copyright 2003 Pearson Education

14 CPS 001 4.14 Origami primitives From Brookshear; Copyright 2003 Pearson Education

15 CPS 001 4.15 Programming Primitive Operations l Assign a value to a variable l Call a method l Arithmetic operation l Comparing two numbers l Indexing into an array l Following an object reference l Returning from a method

16 CPS 001 4.16 Components of Computing Algorithms Any computing algorithm will have AT MOST five kinds of components: Data structures to hold data Instructions change data values Conditional expressions to make decisions Control structures to act on decisions Modules to make the algorithm manageable by abstraction, i.e., grouping related components


Download ppt "CPS 001 4.1 Today’s topics l Algorithms and pseudocode l Notes adapted from Marti Hearst at UC Berkeley and David Smith at Georgia Tech Upcoming ä Beginning."

Similar presentations


Ads by Google