1 / 8

Algorithms

This program gets a value from the user, doubles that value, and returns the result to the user.

blancaf
Télécharger la présentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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 Chapter 5 – Algorithms

  2. 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 Ifday > (number of days in month) Then { Adjust day by subtracting the number of days in month Ifmonth is December Then { Increment year by 1 Set month to January } Else Increment month by one } } Chapter 5 – Algorithms

  3. 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 { Ifyear is a leap year Then increment day_of_week by 2 Else increment day_of_week by 1 Ifday_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? Chapter 5 – Algorithms

  4. 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 Iftest_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. Chapter 5 – Algorithms

  5. 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 Calling SeqSearch(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: 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 Chapter 5 – Algorithms

  6. 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) Ifbase is 0 Then return 0 Else Ifpower < 0 Then return Exponentiate(base, power+1)/base Else Ifpower 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. Chapter 5 – Algorithms

  7. 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 Iftest_name is sought_name Then return corresponding phone number Else Ifphonebook has only one remaining entry Then return “Unlisted” message Iftest_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. Chapter 5 – Algorithms

  8. 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 Calling BinarySearch(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: 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 Chapter 5 – Algorithms

More Related