Algorithms An algorithm is an ordered set of

  • Slides: 8
Download presentation
Algorithms An algorithm is an ordered set of unambiguous, executable steps that ultimately terminate

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: 1. Semantics: The meaning of the step 2. Syntax: The format of the step Semantics Syntax -Get a value from the user Program Double. It; var x, y integer; -Double that value begin write(“Input your number: ”); -Return the result to the user read(x); y = 2*x; writeln(“The doubled number is ”, y); end. CS 111. 01 Chapter 5 – Algorithms 77

Pseudocode is an informal notation for expressing an algorithm. Example: Which years in 2006

Pseudocode is an informal notation for expressing an algorithm. Example: Which years in 2006 -2020 have New Year’s Eve on Saturday? Procedure Sat 1231 A 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 Then { Increment year by 1 Set month to January } Else Increment month by one } } CS 111. 01 Chapter 5 – Algorithms 78

Of course, it is possible to devise many algorithms to solve the same problem.

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 Sat 1231 B 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 79

Iteration (Looping) When an algorithm involves repetitive actions, iteration may be a practical approach.

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 Seq. Search(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 80

Calling Seq. Search(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list

Calling Seq. Search(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: Name Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco Mc. Gonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver CS 111. 01 Number 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 Chapter 5 – Algorithms 81

Recursion (“Divide & Conquer”) Another common approach in algorithms is to employ recursion, which

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 82

A Recursive Search Algorithm Pseudocode to recursively implement the search for a specific name

A Recursive Search Algorithm Pseudocode to recursively implement the search for a specific name in an alphabetized phonebook: Procedure Binary. Search(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 Binary. Search to the portion of phonebook after test_name Else apply Binary. Search 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 83

Calling Binary. Search(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list

Calling Binary. Search(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: Name Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco Mc. Gonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver CS 111. 01 Number 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 2 nd quarter 3. test_name: Cornelius Fudge, so use 4 th eighth 4. test_name: Hermione Granger, so use 8 th sixteenth 5. test_name: Rubeus Hagrid, so return 555 -1317 1. test_name: Gilderoy Lockhart, so use 1 st half Chapter 5 – Algorithms 84