Presentation is loading. Please wait.

Presentation is loading. Please wait.

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:

Similar presentations


Presentation on theme: "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:"— Presentation transcript:

1

2 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: Organize data so it can be accessed and processed efficiently. Organize data so it can be accessed and processed efficiently. Develop algorithms that take advantage of the strengths of the programming language and the hardware to accomplish what the program is attempting to do. Develop algorithms that take advantage of the strengths of the programming language and the hardware to accomplish what the program is attempting to do.

3 Iteration Chapter 9 Abstract Data Types and Algorithms Page 85 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 Set test_name to first name in phonebook While (test_name is alphabetically before sought_name AND While (test_name is alphabetically before sought_name AND there are still more names in phonebook) Do there are still more names in phonebook) Do Set test_name to the next name in phonebook Set test_name to the next name in phonebook If test_name is sought_name If test_name is sought_name Then return the corresponding phone number Then return the corresponding phone number Else return “Unlisted” message Else return “Unlisted” message Procedure SeqSearch(phonebook, sought_name) Set test_name to first name in phonebook Set test_name to first name in phonebook While (test_name is alphabetically before sought_name AND While (test_name is alphabetically before sought_name AND there are still more names in phonebook) Do there are still more names in phonebook) Do Set test_name to the next name in phonebook Set test_name to the next name in phonebook If test_name is sought_name If test_name is sought_name Then return the corresponding phone number Then return the corresponding phone number Else return “Unlisted” message Else return “Unlisted” message When an algorithm involves repetitive actions, iteration (i.e., looping) may be a practical approach. 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.

4 Chapter 9 Abstract Data Types and Algorithms Page 86 Calling SeqSearch(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 test_name is Sirius Black, so iterate again test_name is Cho Chang, so iterate again test_name is Albus Dumbledore, so iterate again test_name is Dudley Dursley, so iterate again test_name is Argus Filch, so iterate again test_name is Cornelius Fudge, so iterate again test_name is Hermione Granger, so iterate again test_name is Rubeus Hagrid, so return 555-1317 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793

5 Chapter 9 Abstract Data Types and Algorithms Page 87 Recursion Pseudocode to recursively take a base number to a specified power: Procedure Exponentiate(base, power) If base is 0 If base is 0 Then return 0 Then return 0 Else If power < 0 Else If power < 0 Then return Exponentiate(base, power+1)/base Then return Exponentiate(base, power+1)/base Else If power is 0 Else If power is 0 Then return 1 Then return 1 Else return base * Exponentiate(base, power-1) Else return base * Exponentiate(base, power-1) Procedure Exponentiate(base, power) If base is 0 If base is 0 Then return 0 Then return 0 Else If power < 0 Else If power < 0 Then return Exponentiate(base, power+1)/base Then return Exponentiate(base, power+1)/base Else If power is 0 Else If power is 0 Then return 1 Then return 1 Else return base * Exponentiate(base, power-1) Else return base * Exponentiate(base, power-1) Another common approach in algorithms is to employ recursion (i.e., “divide and conquer”), which repeatedly reduces the size of a problem until it becomes manageable. 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.

6 Chapter 9 Abstract Data Types and Algorithms Page 88 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 Set test_name to the middle name in phonebook If test_name is sought_name If test_name is sought_name Then return corresponding phone number Then return corresponding phone number Else If phonebook has only one remaining entry Else If phonebook has only one remaining entry Then return “Unlisted” message Then return “Unlisted” message If test_name is alphabetically before sought_name If test_name is alphabetically before sought_name Then apply BinarySearch to the portion of phonebook after test_name Then apply BinarySearch to the portion of phonebook after test_name Else apply BinarySearch to the portion of phonebook before test_name Else apply BinarySearch to the portion of phonebook before test_name Procedure BinarySearch(phonebook, sought_name) Set test_name to the middle name in phonebook Set test_name to the middle name in phonebook If test_name is sought_name If test_name is sought_name Then return corresponding phone number Then return corresponding phone number Else If phonebook has only one remaining entry Else If phonebook has only one remaining entry Then return “Unlisted” message Then return “Unlisted” message If test_name is alphabetically before sought_name If test_name is alphabetically before sought_name Then apply BinarySearch to the portion of phonebook after test_name Then apply BinarySearch to the portion of phonebook after test_name Else apply BinarySearch to the portion of phonebook before 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.

7 Chapter 9 Abstract Data Types and Algorithms Page 89 Chapter 9 Abstract Data Types and Algorithms Page 89 Calling BinarySearch(phonebook, sought_name) where sought_name is “Rubeus Hagrid” and phonebook is the list below: NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 test_name is Gilderoy Lockhart, so iterate again test_name is Rubeus Hagrid, so return 555-1317 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 test_name is Dudley Dursley, so iterate again NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 test_name is Cornelius Fudge, so iterate again NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 test_name is Hermione Granger, so iterate again NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793 NameNameNumberNumber 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 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-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793555-7458555-0131555-3589555-1119555-3783555-9927555-2728555-1317555-1201555-7936555-7174555-1659555-2941555-1503555-8847555-6296555-5165555-6793

8 Chapter 9 Abstract Data Types and Algorithms Page 90 Chapter 9 Abstract Data Types and Algorithms Page 90 Data Structures When interrelated information is stored in a computer’s memory, it is usually convenient for the programmer (and for the computer’s memory management) to keep this data in a structured format. Example: int IQlist[100]; Conceptually, the array looks something like this: Index012…9899 Contents120135116…128133 However, in the computer’s RAM, space for 100 integers has been allocated something like this: AddressContents 00 01 : Memory cell 71 (hex 47) > 4778 4887 4974 :: A980 Memory cell 170 (hex AA) > AA85 : FE FF An array is an indexed list of values of the same type.

9 Chapter 9 Abstract Data Types and Algorithms Page 91 Chapter 9 Abstract Data Types and Algorithms Page 91 Example: int GradeTable[3][5]; Conceptually, the array looks something like this: COLUMN # 01234 ROW # 094891008792 16890847886 277959710088 However, in the computer’s RAM, space for 15 integers has been allocated something like this: AddressContents 00 01 : Space for element (0,0) > B25E (0,1) > B359 (0,2) > B464 (0,3) > B557 (0,4) > B65C (1,0) > B744 (1,1) > B85A (1,2) > B954 (1,3) > BA4E (1,4) > BB56 (2,0) > BC4D (2,1) > BD5F (2,2) > BE61 (2,3) > BF64 (2,4) > C058 : FF A multidimensional array is an indexed table of values of the same type, using more than one dimension.

10 Chapter 9 Abstract Data Types and Algorithms Page 92 Chapter 9 Abstract Data Types and Algorithms Page 92 Rather than reserving a contiguous block of memory to store a list, the linked list dynamically allocates memory as needed for list elements. Example: struct node; typedef node *nodePtr; struct node { int value; int value; nodePtr next; nodePtr next;}; nodePtr List; Conceptually, the linked list looks something like this: 971008894 However, in the computer’s RAM, space for 4 integers has been allocated something like this: AddressContents 00 : 1664 3 rd item is at address B0 17B0 : 4E5E FF signifies the end of List 4FFF : List is located at 9A 9A61 2 nd item is at address 16 9B16 : B058 4 th item is at address 4E B14E : FF

11 Chapter 9 Abstract Data Types and Algorithms Page 93 Chapter 9 Abstract Data Types and Algorithms Page 93 Relative Advantages of Arrays & Linked Lists ArraysArrays Linked Lists  Require contiguous memory  Dynamically locate memory  Requires specific size  Has flexible size  Potentially wastes memory  Only uses allocated space  Potentially runs out of memory  Expands memory as needed  Insertion requires rearranging  Insertion requires slight relink  Deletion requires rearranging  Deletion requires slight relink  One-by-one searching required  Indexing facilitates searching  Sequential search only  Binary search possible if sorted  Tougher to conceptualize  Straightforward to program  Complicated garbage collection  Memory easily cleared after use

12 Chapter 9 Abstract Data Types and Algorithms Page 94 Chapter 9 Abstract Data Types and Algorithms Page 94 Comparison: Retrieving a List from a File Using an array Using a linked list void GetList(int List[50], int &ListSize) int &ListSize){ ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; cout << "Enter the name " cout << "Enter the name " << "of the file: "; << "of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); ListSize = 0; ListSize = 0; file >> val; file >> val; while ((!file.eof()) && while ((!file.eof()) && (ListSize < 50)) (ListSize < 50)) { List[ListSize] = val; List[ListSize] = val; ListSize++; ListSize++; file >> val; file >> val; } file.close(); file.close();} void GetList(int List[50], int &ListSize) int &ListSize){ ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; cout << "Enter the name " cout << "Enter the name " << "of the file: "; << "of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); ListSize = 0; ListSize = 0; file >> val; file >> val; while ((!file.eof()) && while ((!file.eof()) && (ListSize < 50)) (ListSize < 50)) { List[ListSize] = val; List[ListSize] = val; ListSize++; ListSize++; file >> val; file >> val; } file.close(); file.close();} void GetList(nodePtr &List) { ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; nodePtr ptr; nodePtr ptr; cout << "Enter the name " cout << "Enter the name " << “of the file: "; << “of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); List = NULL; List = NULL; file >> val; file >> val; while (!file.eof()) while (!file.eof()) { ptr = new node; ptr = new node; ptr->value = val; ptr->value = val; ptr->next = List; ptr->next = List; List = ptr; List = ptr; file >> val; file >> val; } file.close(); file.close();} void GetList(nodePtr &List) { ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; nodePtr ptr; nodePtr ptr; cout << "Enter the name " cout << "Enter the name " << “of the file: "; << “of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); List = NULL; List = NULL; file >> val; file >> val; while (!file.eof()) while (!file.eof()) { ptr = new node; ptr = new node; ptr->value = val; ptr->value = val; ptr->next = List; ptr->next = List; List = ptr; List = ptr; file >> val; file >> val; } file.close(); file.close();} Extra concern: Exceeding array’s size Extra concern: Allocating new memory void GetList(int List[50], int &ListSize) int &ListSize){ ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; cout << "Enter the name " cout << "Enter the name " << "of the file: "; << "of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); ListSize = 0; ListSize = 0; file >> val; file >> val; while ((!file.eof()) && while ((!file.eof()) && (ListSize < 50)) (ListSize < 50)) { List[ListSize] = val; List[ListSize] = val; ListSize++; ListSize++; file >> val; file >> val; } file.close(); file.close();} void GetList(int List[50], int &ListSize) int &ListSize){ ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; cout << "Enter the name " cout << "Enter the name " << "of the file: "; << "of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); ListSize = 0; ListSize = 0; file >> val; file >> val; while ((!file.eof()) && while ((!file.eof()) && (ListSize < 50)) (ListSize < 50)) { List[ListSize] = val; List[ListSize] = val; ListSize++; ListSize++; file >> val; file >> val; } file.close(); file.close();} void GetList(nodePtr &List) { ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; nodePtr ptr; nodePtr ptr; cout << "Enter the name " cout << "Enter the name " << “of the file: "; << “of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); List = NULL; List = NULL; file >> val; file >> val; while (!file.eof()) while (!file.eof()) { ptr = new node; ptr = new node; ptr->value = val; ptr->value = val; ptr->next = List; ptr->next = List; List = ptr; List = ptr; file >> val; file >> val; } file.close(); file.close();} void GetList(nodePtr &List) { ifstream file; ifstream file; char fileName[50]; char fileName[50]; int val; int val; nodePtr ptr; nodePtr ptr; cout << "Enter the name " cout << "Enter the name " << “of the file: "; << “of the file: "; cin >> fileName; cin >> fileName; file.open(fileName); file.open(fileName); assert(!file.fail()); assert(!file.fail()); List = NULL; List = NULL; file >> val; file >> val; while (!file.eof()) while (!file.eof()) { ptr = new node; ptr = new node; ptr->value = val; ptr->value = val; ptr->next = List; ptr->next = List; List = ptr; List = ptr; file >> val; file >> val; } file.close(); file.close();}

13 Chapter 9 Abstract Data Types and Algorithms Page 95 Chapter 9 Abstract Data Types and Algorithms Page 95 Comparison: Sequential Search Using an array Using a linked list int Search(int List[50], int ListSize, int ListSize, int soughtVal) int soughtVal){ int count; int count; bool found = false; bool found = false; count = 0; count = 0; while ((!found) && while ((!found) && (count < 50)) (count < 50)) { if (List[count] == if (List[count] == soughtVal) soughtVal) found = true; found = true; else else count++; count++; } if (found) if (found) return List[count]; return List[count]; else else return -1; return -1;} int Search(int List[50], int ListSize, int ListSize, int soughtVal) int soughtVal){ int count; int count; bool found = false; bool found = false; count = 0; count = 0; while ((!found) && while ((!found) && (count < 50)) (count < 50)) { if (List[count] == if (List[count] == soughtVal) soughtVal) found = true; found = true; else else count++; count++; } if (found) if (found) return List[count]; return List[count]; else else return -1; return -1;} int Search(nodePtr List, int soughtVal) int soughtVal){ nodePtr currPtr; nodePtr currPtr; bool found = false; bool found = false; currPtr = List; currPtr = List; while ((!found) && while ((!found) && (currPtr != NULL)) (currPtr != NULL)) { if (currPtr->value == if (currPtr->value == soughtVal) soughtVal) found = true; found = true; else else currPtr = currPtr->next; currPtr = currPtr->next; } if (found) if (found) return currPtr->value; return currPtr->value; else else return -1 return -1} int Search(nodePtr List, int soughtVal) int soughtVal){ nodePtr currPtr; nodePtr currPtr; bool found = false; bool found = false; currPtr = List; currPtr = List; while ((!found) && while ((!found) && (currPtr != NULL)) (currPtr != NULL)) { if (currPtr->value == if (currPtr->value == soughtVal) soughtVal) found = true; found = true; else else currPtr = currPtr->next; currPtr = currPtr->next; } if (found) if (found) return currPtr->value; return currPtr->value; else else return -1 return -1} Note again that the code is almost identical, but the array version is limited to lists of a certain size. If the list is too long, the array can’t hold it all; if it’s too short, several memory slots are wasted.

14 Chapter 9 Abstract Data Types and Algorithms Page 96 Chapter 9 Abstract Data Types and Algorithms Page 96 Sorting Algorithms Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Somewhat more complicated than searching an alphabetized list is the problem of alphabetizing such a list to begin with. Numerous sorting algorithms have been developed, each with its own advantages and disadvantages with respect to: Speed with which it sorts a completely random list Speed with which it sorts a completely random list Speed with which it sorts a nearly sorted list Speed with which it sorts a nearly sorted list Amount of memory required to implement it Amount of memory required to implement it Ease with which it can be coded Ease with which it can be coded Examination of three such algorithms follows, with each algorithm applied to the following list of 26 three-letter names:

15 Chapter 9 Abstract Data Types and Algorithms Page 97 Chapter 9 Abstract Data Types and Algorithms Page 97 Selection Sort 1.Let k equal the size of your list 2.Let i equal the index of the first element of your list 3.Swap the smallest element in the last k elements with the i th element 4.Decrease k by one 5.Increase i by one 6.If k is still larger than one, repeat, starting at step #3 1.Let k equal the size of your list 2.Let i equal the index of the first element of your list 3.Swap the smallest element in the last k elements with the i th element 4.Decrease k by one 5.Increase i by one 6.If k is still larger than one, repeat, starting at step #3 Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Ann Edy Zeb Ort Bob Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Ann Bob Zeb Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Ann Bob Cub Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Dan Lex Pez Hal Tia Ann Bob Cub Dan Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia Ann Bob Cub Dan Edy Fly Moe Uma Quo Kit Wes Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia Ann Bob Cub Dan Edy Fly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb  Verdict: Easy to program, little memory waste, very inefficient

16 Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Chapter 9 Abstract Data Types and Algorithms Page 98 Chapter 9 Abstract Data Types and Algorithms Page 98 Bubble Sort 1.Let k equal the size of your list 2.Let i equal the index of the first element of your list 3.Starting with the i th element of the list and moving down to the k th element, swap every consecutive pair of elements that is in the wrong order 4.Decrease k by one 5.Increase i by one 6.If k is still larger than one, repeat, starting at step #3 1.Let k equal the size of your list 2.Let i equal the index of the first element of your list 3.Starting with the i th element of the list and moving down to the k th element, swap every consecutive pair of elements that is in the wrong order 4.Decrease k by one 5.Increase i by one 6.If k is still larger than one, repeat, starting at step #3 Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia  Verdict: Tougher to program, little memory waste, inefficient in general (but could easily be modified to terminate early if a swap-less pass occurs) Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Ort Zeb Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Zeb  Edy Moe Bob Ort Ann Uma Quo Kit Fly Vin Wes Gus Joe Nan Sue Cub Ida Xon Ren Dan Lex Pez Hal Tia Yul Zeb Ann Bob Cub Dan Edy Fly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb 

17 Chapter 9 Abstract Data Types and Algorithms Page 99 Chapter 9 Abstract Data Types and Algorithms Page 99 Quick Sort 1.Let leftIndex be the index of the leftmost element of an unsorted portion of the list and rightIndex be the index of the rightmost element of that portion of the list 2.Let pivot equal the value currently at index p of the list 3.Moving in from the rightIndex element of the list, keep moving until a value less than pivot is found; set rightIndex to the index of that value and insert it at position leftIndex 4.Moving in from the leftIndex element of the list, keep moving until a value greater than pivot is found; set leftIndex to the index of that value and insert it at position rightIndex 5.If leftIndex doesn’t equal rightIndex, return to step #3; otherwise, insert pivot at index leftIndex and return to step #1, starting over with another unsorted portion of the list 1.Let leftIndex be the index of the leftmost element of an unsorted portion of the list and rightIndex be the index of the rightmost element of that portion of the list 2.Let pivot equal the value currently at index p of the list 3.Moving in from the rightIndex element of the list, keep moving until a value less than pivot is found; set rightIndex to the index of that value and insert it at position leftIndex 4.Moving in from the leftIndex element of the list, keep moving until a value greater than pivot is found; set leftIndex to the index of that value and insert it at position rightIndex 5.If leftIndex doesn’t equal rightIndex, return to step #3; otherwise, insert pivot at index leftIndex and return to step #1, starting over with another unsorted portion of the list Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Moe pivot: Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaHal Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Hal Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Zeb Tia Hal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Zeb Tia Hal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Ort Pez Zeb Tia Hal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Ort Pez Zeb Tia Hal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus Joe Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Xon Gus Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Xon Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal pivot: Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaGus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Xon pivot: Gus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaGus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Tia Vin Nan Sue Quo Uma Pez Ren Wes Ort Xon Zeb Yul   Ann Bob Cub Dan Edy Fly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb  Verdict: Much tougher to program, little memory waste, efficient in general (but very inefficient if the list is already almost sorted)

18 Chapter 9 Abstract Data Types and Algorithms Page 100 Chapter 9 Abstract Data Types and Algorithms Page 100 Example: A stack is a data structure that manages a list of similar items in such a way that all insertions and deletions take place at one designated end of the list. In effect, one end of the list is considered the “top” of the stack, inserting into the list is considered “pushing” an item onto the top of the stack, and deleting from the list is considered “popping” off the top of the stack. Initial Stack 3 After “Push 3” 53 After “Push 5” 853 After “Push 8” 53 After “Pop” 3 13 After “Push 1”

19 Chapter 9 Abstract Data Types and Algorithms Page 101 Chapter 9 Abstract Data Types and Algorithms Page 101 Comparison: Stack Implementations Using an array Using a linked list void Push(int List[50], int &Top, int &Top, int item) int item){ if (Top < 49) if (Top < 49) { Top++; Top++; List[Top] = item; List[Top] = item; }} int Pop(int List[50], int &Top) int &Top){ int val = -1; int val = -1; if (Top >= 0) if (Top >= 0) { val = List[Top]; val = List[Top]; Top--; Top--; } return val; return val;} void Push(int List[50], int &Top, int &Top, int item) int item){ if (Top < 49) if (Top < 49) { Top++; Top++; List[Top] = item; List[Top] = item; }} int Pop(int List[50], int &Top) int &Top){ int val = -1; int val = -1; if (Top >= 0) if (Top >= 0) { val = List[Top]; val = List[Top]; Top--; Top--; } return val; return val;} void Push(nodePtr &List, int item) int item){ nodePtr ptr = new node; nodePtr ptr = new node; ptr->value = item; ptr->value = item; ptr->next = List; ptr->next = List; List = ptr; List = ptr;} int Pop(nodePtr &List) { int val = -1; int val = -1; if (nodePtr != NULL) if (nodePtr != NULL) { val = nodePtr->value; val = nodePtr->value; List = List->next; List = List->next; } return val; return val;} void Push(nodePtr &List, int item) int item){ nodePtr ptr = new node; nodePtr ptr = new node; ptr->value = item; ptr->value = item; ptr->next = List; ptr->next = List; List = ptr; List = ptr;} int Pop(nodePtr &List) { int val = -1; int val = -1; if (nodePtr != NULL) if (nodePtr != NULL) { val = nodePtr->value; val = nodePtr->value; List = List->next; List = List->next; } return val; return val;}

20 Chapter 9 Abstract Data Types and Algorithms Page 102 Chapter 9 Abstract Data Types and Algorithms Page 102 Example Stack Application Main: line #3 x:0 y:-2 z:? When Main reaches the A(); step A: line #4 i:10 j:46 k:31 Main: line #3 x:0 y:-2 z:? When A reaches the B(); step B: line #3 r:400 s:542 t:? A: line #4 i:10 j:46 k:31 Main: line #3 x:0 y:-2 z:? When B reaches the C(); step When C finishes, the stack is popped and B resumes. When B finishes, the stack is popped and A resumes. When A finishes, the stack is popped and Main resumes and finishes. Keeping track of function calls in a third-generation programming language. Main Program x = 0; y = -2; A(); z = 6; cout << x << y << z << endl; << z << endl; x = 0; y = -2; A(); z = 6; cout << x << y << z << endl; << z << endl; Subprogram A() i = 10; j = 46; k = 31; B(); j = 50; cout << i << j << k << endl; << k << endl; i = 10; j = 46; k = 31; B(); j = 50; cout << i << j << k << endl; << k << endl; Subprogram B() r = 400; s = 542; C(); r = 710; s = 365; r = 927; cout << r << s << t << endl; << t << endl; r = 400; s = 542; C(); r = 710; s = 365; r = 927; cout << r << s << t << endl; << t << endl; Subprogram C() u = 15; v = 57; w = 34; cout << u << v << w << endl; << w << endl; u = 15; v = 57; w = 34; cout << u << v << w << endl; << w << endl; x = 0; y = -2; A(); z = 6; cout << x << y << z << endl; << z << endl; x = 0; y = -2; A(); z = 6; cout << x << y << z << endl; << z << endl; i = 10; j = 46; k = 31; B(); j = 50; cout << i << j << k << endl; << k << endl; i = 10; j = 46; k = 31; B(); j = 50; cout << i << j << k << endl; << k << endl; r = 400; s = 542; C(); r = 710; s = 365; r = 927; cout << r << s << t << endl; << t << endl; r = 400; s = 542; C(); r = 710; s = 365; r = 927; cout << r << s << t << endl; << t << endl;

21 Chapter 9 Abstract Data Types and Algorithms Page 103 Chapter 9 Abstract Data Types and Algorithms Page 103 Example: A queue is a data structure that manages a list of similar items in such a way that all insertions take place at one end of the list, while all deletions take place at the other end. In effect, one end of the list is considered the “rear” of the queue, where new items enter; and the other end is considered the “front” of the queue, where old items are removed. Initial Queue: F/R After Insert 7: 7 FR After Insert 4: 74 FR After Insert 2: 742 FR After Remove: 42 FR After Insert 5: 425

22 Chapter 9 Abstract Data Types and Algorithms Page 104 Chapter 9 Abstract Data Types and Algorithms Page 104 Comparison: Queue Implementations Using an array Using a linked list void Insert(int List[50], int &Front, int &Rear, int item) { if (Front != (Rear+1)%50) { Rear = (Rear+1)%50; List[Rear] = item; if (Front == -1) Front = Rear; } int Remove(int List[50], int &Front, int &Rear) { int val = -1; if (Front > -1) { val = List[Front]; if (Front == Rear) Front = Rear = -1; else Front = (Front+1)%50; } return val; } void Insert(int List[50], int &Front, int &Rear, int item) { if (Front != (Rear+1)%50) { Rear = (Rear+1)%50; List[Rear] = item; if (Front == -1) Front = Rear; } int Remove(int List[50], int &Front, int &Rear) { int val = -1; if (Front > -1) { val = List[Front]; if (Front == Rear) Front = Rear = -1; else Front = (Front+1)%50; } return val; } void Insert(nodePtr &ListFront, nodePtr &ListRear, int item) { nodePtr ptr = new node; ptr->value = item; ptr->next = NULL; if (ListFront == NULL) ListFront = ptr; else ListRear->next = ptr; ListRear = ptr; } int Remove(nodePtr &ListFront, nodePtr &ListRear) { int val = -1; if (ListFront != NULL) { val = ListFront->value; ListFront = ListFront->next; } if (ListFront == NULL) ListRear = NULL; return val; } void Insert(nodePtr &ListFront, nodePtr &ListRear, int item) { nodePtr ptr = new node; ptr->value = item; ptr->next = NULL; if (ListFront == NULL) ListFront = ptr; else ListRear->next = ptr; ListRear = ptr; } int Remove(nodePtr &ListFront, nodePtr &ListRear) { int val = -1; if (ListFront != NULL) { val = ListFront->value; ListFront = ListFront->next; } if (ListFront == NULL) ListRear = NULL; return val; }

23 Chapter 9 Abstract Data Types and Algorithms Page 105 Chapter 9 Abstract Data Types and Algorithms Page 105 Example Queue Application Keeping track of batch jobs as they arrive to be processed by a computer. Job A arrives and starts processing: CPU processing Job A Job Queue: Job B arrives: CPU processing Job A Job Queue: B Jobs C & D arrive: CPU processing Job A Job Queue: BCD Job A completes; Job B starts processing: Job Queue: CD CPU processing Job B

24 Chapter 9 Abstract Data Types and Algorithms Page 106 Chapter 9 Abstract Data Types and Algorithms Page 106 Example Implementation: struct node; typedef node *nodePtr; struct node { int value; int value; nodePtr left; nodePtr left; nodePtr right; nodePtr right;}; nodePtr Tree; A binary tree is a hierarchical data structure that manages a collection of similar items in such a way that one item is designated as the “root” of the tree, and every other item is either the left or right “offspring” of some previously positioned item. Example: Binary Insertion Tree Each left offspring of a node has a value less than the node’s valueEach left offspring of a node has a value less than the node’s value Each right offspring of a node has a value greater than or equal to the node’s valueEach right offspring of a node has a value greater than or equal to the node’s value

25 Chapter 9 Abstract Data Types and Algorithms Page 107 Chapter 9 Abstract Data Types and Algorithms Page 107 Recursive Insertion into a Binary Insertion Tree Example: Where will a new node containing the integer 11 be inserted? void Bin_Insert(nodePtr &Tree, int item) { if (Tree == NULL) if (Tree == NULL) { nodePtr ptr = new node; nodePtr ptr = new node; ptr->value = item; ptr->value = item; ptr->left = NULL; ptr->left = NULL; ptr->right = NULL; ptr->right = NULL; } else if (item value) else if (item value) Bin_Insert(Tree->left, item); Bin_Insert(Tree->left, item); else else Bin_Insert(Tree->right, item); Bin_Insert(Tree->right, item);} void Bin_Insert(nodePtr &Tree, int item) { if (Tree == NULL) if (Tree == NULL) { nodePtr ptr = new node; nodePtr ptr = new node; ptr->value = item; ptr->value = item; ptr->left = NULL; ptr->left = NULL; ptr->right = NULL; ptr->right = NULL; } else if (item value) else if (item value) Bin_Insert(Tree->left, item); Bin_Insert(Tree->left, item); else else Bin_Insert(Tree->right, item); Bin_Insert(Tree->right, item);}

26 Chapter 9 Abstract Data Types and Algorithms Page 108 Chapter 9 Abstract Data Types and Algorithms Page 108 Recursive Traversal of a Binary Insertion Tree Example: Apply Inorder to this binary insertion tree: void Inorder(nodePtr Tree) { if (Tree != NULL) if (Tree != NULL) { Inorder(Tree->left); Inorder(Tree->left); cout value value << endl; Inorder(Tree->right); Inorder(Tree->right); }} void Inorder(nodePtr Tree) { if (Tree != NULL) if (Tree != NULL) { Inorder(Tree->left); Inorder(Tree->left); cout value value << endl; Inorder(Tree->right); Inorder(Tree->right); }}

27 Chapter 9 Abstract Data Types and Algorithms Page 109 Chapter 9 Abstract Data Types and Algorithms Page 109 What Does This Function Do To A Binary Tree? int Sumac(nodePtr Tree) { int leftbranch, rightbranch; int leftbranch, rightbranch; if (Tree == NULL) if (Tree == NULL) return 0; return 0; else else { leftbranch = Sumac(Tree->left); leftbranch = Sumac(Tree->left); rightbranch = Sumac(Tree->right); rightbranch = Sumac(Tree->right); return leftbranch + rightbranch + Tree->value; return leftbranch + rightbranch + Tree->value; }} int Sumac(nodePtr Tree) { int leftbranch, rightbranch; int leftbranch, rightbranch; if (Tree == NULL) if (Tree == NULL) return 0; return 0; else else { leftbranch = Sumac(Tree->left); leftbranch = Sumac(Tree->left); rightbranch = Sumac(Tree->right); rightbranch = Sumac(Tree->right); return leftbranch + rightbranch + Tree->value; return leftbranch + rightbranch + Tree->value; }} 00 34 00 22 61 0 00 9 00 15 31 51 125


Download ppt "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:"

Similar presentations


Ads by Google