Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 4 Computation Bjarne Stroustrup
MATH 224 – Discrete Mathematics
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS 280 Data Structures Professor John Peterson. Project Questions?
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Analysis of Recursive Algorithms
Data Structures Review Session 1
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Recursion.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Controlling Program Flow with Decision Structures.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Vishnu Kotrajaras, PhD.1 Data Structures
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Week 13: Searching and Sorting
Debugging and Random Numbers
Week 6 - Wednesday CS221.
Data Structures in Java with JUnit ©Rick Mercer
Week 12 - Wednesday CS221.
CSE 1342 Programming Concepts
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Selection Insertion and Merge
CSE 373 Data Structures and Algorithms
24 Searching and Sorting.
Recap Week 2 and 3.
Building Java Programs
Recursion Chapter 11.
Week 6 - Monday CS221.
Presentation transcript:

Week 6 - Wednesday

 What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Recursion

Infix to Postfix Converter

1. a = b;  a and b have to have the same type  Exception: b is a subtype of a 2. == is a comparison operator that produces a boolean, = is a left- pointing arrow 3. if statements always have parentheses 4. No semicolons after if or while headers (and rarely after for headers) 5. The following is always wrong: x = y; x = z; 6. Know your String methods 7. A non- void method has to return something 8. The new keyword is always followed by a type and either parentheses (possibly with arguments) or square brackets with integers inside 9. Local variables cannot be declared private, public, or protected

 By now, you should know your syntax inside and out  If you don't know how something in Java works, find out!  Syntax is your basic tool for everything  Read about some syntax once? Doesn't help! Write some code to see it in practice! Syntax Problem Solving Design CS121CS122 CS221

 We want to raise a number x to a power n, like so: x n  We allow x to be real, but n must be an integer greater than or equal to 0  Example: (4.5) 13 =

 Base case (n = 0):  Result = 1  Recursive case (n > 0):  Result = x ∙ x (n – 1)

public static double power( double x, int n ) { if( n == 0 ) return 1; else return x * power( x, n – 1); } Base Case Recursive Case

 Each call reduces n by 1  n total calls  What's the running time? Θ(n)Θ(n)

 We need to structure the recursion differently  Instead of reducing n by 1 each time, can we reduce it by a lot more?  It’s true that x n = x ∙ x (n – 1)  But, it is also true that x n = x (n/2) ∙ x (n/2)

 Assume that n is a power of 2  Base case (n = 1):  Result = x  Recursive case (n > 1):  Result = (x (n/2) ) 2

public static double power2( double x, int n ) { double temp; if( n == 1 ) return x; else { temp = power2( x, n/2 ); return temp * temp; } } Base Case Recursive Case

 Each call reduces n by half  log 2 (n) total calls  Just like binary search  Can we expand the algorithm to even and odd values of n?

 Base case (n = 1):  Result = x  Recursive cases (n > 1):  If n is even, result = (x (n/2) ) 2  If n is odd, result = x ∙ (x ((n – 1)/2) ) 2

public static double power3( double x, int n ) { double temp; if( n == 1 ) return x; else if( n % 2 == 0 ) { temp = power3( x, n/2 ); return temp * temp; } else { temp = power3( x, (n – 1)/2 ); return x * temp * temp; } } Base Case Recursive Cases

 Beautiful divide and conquer  Base case: List has size 1  Recursive case:  Divide your list in half  Recursively merge sort each half  Merge the two halves back together in sorted order  But how long does it take?

 Has a great name…  Allows us to determine the Big Theta running time of many recursive functions that would otherwise be difficult to determine

 a is the number of recursive calls made  b is how much the quantity of data is divided by each recursive call  f(n) is the non-recursive work done at each step

 Base case: List has size less than 3  Recursive case:  Recursively sort the first 2/3 of the list  Recursively sort the second 2/3 of the list  Recursively sort the first 2/3 of the list again

 We need to know log b a  a = 3  b = 3/2 = 1.5  Because I’m a nice guy, I’ll tell you that the log is about 2.7

 We know that binary search takes O(log n) time  Can we use the Master Theorem to check that?

 More on the Master theorem  Symbol tables  Read Chapter 3.1

 Finish Assignment 3  Due Friday, October 2, 2015  Keep working on Project 2  Due Friday, October 9, 2015