Every step in an algorithm has two basic components:

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Chapter 6: Low-Level Programming Languages Chapter 6 Low-Level Programming Languages Page 51 In order to execute instructions on a CPU, those instructions.
MATH 224 – Discrete Mathematics
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Chapter 11 Chapter 11.
Chapter 19 Recursion.
Recursion.
Structured Program Development in C
Adapted from slides by Marie desJardins
Chapter 9: Abstract Data Types and Algorithms Chapter 9 Abstract Data Types and Algorithms Page 84 Two keys to making computer software that works well:
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
“The study of algorithms is the cornerstone of computer science.” Algorithms Fall 2011.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.

Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 19 Searching and Sorting
Searching and Sorting Searching algorithms with simple arrays
How to use the mobile friendly
Fundamentals of Algorithms MCS - 2 Lecture # 11
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
16 Searching and Sorting.

Chapter 6: Loops.
Chapter 15 Recursion.
while Repetition Structure
Genetic Traits in Harry Potter
COP 3503 FALL 2012 Shayan Javed Lecture 15
Control Structures II Chapter 3
Chapter 3: Decisions and Loops
Chapter 15 Recursion.
GC211Data Structure Lecture2 Sara Alhajjam.
“The Boy who lived” The Theme for this Assignment will be chosen from the Harry Potter series by author, JK Rowling.
The Selection Structure
Programming Fundamentals
Algorithm and Ambiguity
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Outline Altering flow of control Boolean expressions
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Introduction to Algorithms and Programming
Algorithm and Ambiguity
Harry Potter.
Basics of Recursion Programming with Recursion
CSCE 222 Discrete Structures for Computing
And now for something completely different . . .
The structure of programming
Thinking procedurally
Chapter 13 Conditional Repetition
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Every step in an algorithm has two basic components: Algorithms An algorithm is an ordered set of unambiguous, executable steps that ultimately terminate if followed. In computer programming, an algorithm is the sequence of steps (i.e., the “recipe”) for accomplishing a task. Every step in an algorithm has two basic components: Semantics: The meaning of the step Syntax: The format of the step Semantics Syntax Get a value from the user Double that value Return the result to the user Program DoubleIt; var x, y integer; begin write(“Input your number: ”); read(x); y = 2*x; writeln(“The doubled number is ”, y); end. CS 111.01 Chapter 5 – Algorithms

Pseudocode Pseudocode is an informal notation for expressing an algorithm. Example: Which years in 2006-2020 have New Year’s Eve on Saturday? Procedure Sat1231A Set year to 2006 Set month to January Set day to first Saturday in January 2006 While (year < 2021) Do { Increment day by 7 If date is New Year’s Eve Then display year as having a Saturday New Year’s Eve If day > (number of days in month) Then Adjust day by subtracting the number of days in month If month is December Increment year by 1 } Else Increment month by one CS 111.01 Chapter 5 – Algorithms

Of course, it is possible to devise many algorithms to solve the same problem. Alternative pseudocode to determine which years in 2006-2020 have New Year’s Eve on Saturday: Procedure Sat1231B Set day_of_week to 12/31/2005 Set year to 2006 While (year < 2021) Do { If year is a leap year Then increment day_of_week by 2 Else increment day_of_week by 1 If day_of_week is Saturday Then display year as having a Saturday New Year’s Eve Increment year by 1 } Both algorithms work, but which is better? Which is easier to code? Which runs more efficiently? CS 111.01 Chapter 5 – Algorithms

Iteration (Looping) When an algorithm involves repetitive actions, iteration may be a practical approach. Pseudocode to implement the search for a specific name in an alphabetized phonebook: Procedure SeqSearch(phonebook, sought_name) Set test_name to first name in phonebook While (test_name is alphabetically before sought_name AND there are stillmore names in phonebook) Do Set test_name to the next name in phonebook If test_name is sought_name Then return the corresponding phone number Else return “Unlisted” message Notice that this algorithm always starts at the top of the phonebook list and checks each name against sought_name until it either locates it or (if it’s not in the phonebook) passes it. CS 111.01 Chapter 5 – Algorithms

Calling SeqSearch(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: Name Number Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 1. test_name is Sirius Black, so iterate again 2. test_name is Cho Chang, so iterate again 3. test_name is Albus Dumbledore, so iterate again 4. test_name is Dudley Dursley, so iterate again 5. test_name is Argus Filch, so iterate again 6. test_name is Cornelius Fudge, so iterate again 7. test_name is Hermione Granger, so iterate again 8. test_name is Rubeus Hagrid, so return 555-1317 CS 111.01 Chapter 5 – Algorithms

Recursion (“Divide & Conquer”) Another common approach in algorithms is to employ recursion, which repeatedly reduces the size of a problem until it becomes manageable. Pseudocode to recursively take a base number to a specified power: Procedure Exponentiate(base, power) If base is 0 Then return 0 Else If power < 0 Then return Exponentiate(base, power+1)/base Else If power is 0 Then return 1 Else return base * Exponentiate(base, power-1) Notice that this algorithm returns 0 if the value of base is 0, 1 if the value of power is 0, base if the value of power is 1, 1/base if the value of power is -1, and so on. CS 111.01 Chapter 5 – Algorithms

A Recursive Search Algorithm Pseudocode to recursively implement the search for a specific name in an alphabetized phonebook: Procedure BinarySearch(phonebook, sought_name) Set test_name to the middle name in phonebook If test_name is sought_name Then return corresponding phone number Else If phonebook has only one remaining entry Then return “Unlisted” message If test_name is alphabetically before sought_name Then apply BinarySearch to the portion of phonebook after test_name Else apply BinarySearch to the portion of phonebook before test_name Notice that this algorithm starts at the middle of the phonebook list, and keeps splitting what’s left of the phonebook in half until it either locates sought_name or runs out of names to check. CS 111.01 Chapter 5 – Algorithms

Calling BinarySearch(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: Name Number Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 2. test_name: Dudley Dursley, so use 2nd quarter 3. test_name: Cornelius Fudge, so use 4th eighth 4. test_name: Hermione Granger, so use 8th sixteenth 5. test_name: Rubeus Hagrid, so return 555-1317 1. test_name: Gilderoy Lockhart, so use 1st half CS 111.01 Chapter 5 – Algorithms