1 / 26

Chapter 9: Abstract Data Types and Algorithms

Chapter 9: Abstract Data Types and Algorithms. Two keys to making computer software that works well:. Organize data so it can be accessed and processed efficiently.

Download Presentation

Chapter 9: Abstract Data Types and 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. Chapter 9: Abstract Data Types and Algorithms Two keys to making computer software that works well: • 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. Chapter 9 Abstract Data Types and Algorithms Page 84

  2. Iteration When an algorithm involves repetitive actions, iteration (i.e., looping) 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 still more 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. Chapter 9 Abstract Data Types and Algorithms Page 85

  3. Name Name Name Name Name Name Name Name Name Number Number Number Number Number Number Number Number 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 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 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 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 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-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 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 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 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 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 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 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 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 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 “RubeusHagrid” and phonebook is the list below: 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 RubeusHagrid, so return 555-1317 Chapter 9 Abstract Data Types and Algorithms Page 86

  4. Recursion 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. 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. Chapter 9 Abstract Data Types and Algorithms Page 87

  5. 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. Chapter 9 Abstract Data Types and Algorithms Page 88

  6. Name Name Name Name Name Name Name Name Name Name Number Number Number Number Number Number Number Number Number 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 “RubeusHagrid” and phonebook is the list below: test_name is Dudley Dursley, so iterate again test_name is Cornelius Fudge, so iterate again test_name is Hermione Granger, so iterate again test_name is RubeusHagrid, so return 555-1317 test_name is Gilderoy Lockhart, so iterate again Chapter 9 Abstract Data Types and Algorithms Page 89 Chapter 9 Abstract Data Types and Algorithms Page 89

  7. 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. However, in the computer’s RAM, space for 100 integers has been allocated something like this: Data Structure #1: The Array An array is an indexed list of values of the same type. Example: intIQlist[100]; Conceptually, the array looks something like this: Chapter 9 Abstract Data Types and Algorithms Page 90 Chapter 9 Abstract Data Types and Algorithms Page 90

  8. Data Structure #2: The MultidimensionalArray A multidimensional array is an indexed table of values of the same type, using more than one dimension. Example: intGradeTable[3][5]; Conceptually, the array looks something like this: However, in the computer’s RAM, space for 15 integers has been allocated something like this: Chapter 9 Abstract Data Types and Algorithms Page 91 Chapter 9 Abstract Data Types and Algorithms Page 91

  9. Rather than reserving a contiguous block of memory to store a list, the linked list dynamically allocates memory as needed for list elements. Data Structure #3: The Linked List Example: struct node; typedef node *nodePtr; struct node { int value; nodePtr next; }; nodePtr List; However, in the computer’s RAM, space for 4 integers has been allocated something like this: Conceptually, the linked list looks something like this: Chapter 9 Abstract Data Types and Algorithms Page 92 Chapter 9 Abstract Data Types and Algorithms Page 92

  10. Relative Advantages of Arrays & Linked Lists Arrays 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 • Indexing facilitates searching • One-by-one searching required • Binary search possible if sorted • Sequential search only • Straightforward to program • Tougher to conceptualize • Memory easily cleared after use • Complicated garbage collection Chapter 9 Abstract Data Types and Algorithms Page 93 Chapter 9 Abstract Data Types and Algorithms Page 93

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

  12. Comparison: Sequential Search Using an array Using a linked list int Search(int List[50], intListSize, intsoughtVal) { int count; bool found = false; count = 0; while ((!found) && (count < 50)) { if (List[count] == soughtVal) found = true; else count++; } if (found) return List[count]; else return -1; } int Search(nodePtr List, intsoughtVal) { nodePtrcurrPtr; bool found = false; currPtr = List; while ((!found) && (currPtr != NULL)) { if (currPtr->value == soughtVal) found = true; else currPtr = currPtr->next; } if (found) return currPtr->value; else 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. Chapter 9 Abstract Data Types and Algorithms Page 95 Chapter 9 Abstract Data Types and Algorithms Page 95

  13. Sorting Algorithms 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 nearly sorted list • Amount of memory required to implement it • 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: 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 Chapter 9 Abstract Data Types and Algorithms Page 96 Chapter 9 Abstract Data Types and Algorithms Page 96

  14. Selection Sort Let k equal the size of your list Let i equal the index of the first element of your list Swap the smallest element in the last k elements with the ith element Decrease k by one Increase i by one 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 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 AnnEdy 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 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 AnnBobZeb Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia AnnBob Zeb Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia AnnBobCubOrt Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Dan Lex Pez Hal Tia AnnBobCub Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Dan Lex Pez Hal Tia AnnBobCubDan Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdyWes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdyFly Moe Uma Quo Kit Wes Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia  AnnBobCubDanEdyFly 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 Chapter 9 Abstract Data Types and Algorithms Page 97 Chapter 9 Abstract Data Types and Algorithms Page 97

  15. Bubble Sort Let k equal the size of your list Let i equal the index of the first element of your list Starting with the ith element of the list and moving down to the kth element, swap every consecutive pair of elements that is in the wrong order Decrease k by one Increase i by one 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 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 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 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 YulZeb  AnnBobCubDanEdyFly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb Verdict: Tougher to program, little memory waste, inefficient in general (but could easily be modified to terminate early if a swap-less pass occurs) Chapter 9 Abstract Data Types and Algorithms Page 98 Chapter 9 Abstract Data Types and Algorithms Page 98

  16. Quick Sort 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 Let pivot equal the value currently at index p of the list 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 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 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 pivot: Moe Hal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren DanOrt Pez Zeb Tia 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 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 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 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 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 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 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 Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren DanOrt 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 Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren DanOrt Pez Zeb 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 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 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 Cub Joe Kit Fly Gus Moe Xon Vin 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 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 Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue CubUma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue CubUma 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 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 Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue CubUma 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 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 Cub Joe Kit Fly Vin Xon GusVin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Xon GusVin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus XonGusVin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus XonXon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia  pivot: Hal 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 Tia HalEdy Lex Dan Bob Ida Ann Cub Joe Kit Fly GusMoe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia  pivot: Xon 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 Tia Gus 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  AnnBobCubDanEdyFly 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) Chapter 9 Abstract Data Types and Algorithms Page 99 Chapter 9 Abstract Data Types and Algorithms Page 99

  17. Data Structure #4: The Stack 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. Example: Chapter 9 Abstract Data Types and Algorithms Page 100 Chapter 9 Abstract Data Types and Algorithms Page 100

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

  19. Example Stack Application Keeping track of function calls in a third-generation programming language. Main Program Subprogram A() Subprogram B() Subprogram C() x = 0; y = -2; A(); z = 6; cout << x << y << z << endl; i = 10; j = 46; k = 31; B(); j = 50; cout << i << j << k << endl; r = 400; s = 542; C(); r = 710; s = 365; r = 927; cout << r << s << t << endl; u = 15; v = 57; w = 34; cout << u << v << w << endl; x = 0; y = -2; A(); z = 6; cout << x << y << z << endl; i = 10; j = 46; k = 31; B(); j = 50; cout << i << j << k << endl; r = 400; s = 542; C(); r = 710; s = 365; r = 927; cout << r << s << t << endl; 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. Chapter 9 Abstract Data Types and Algorithms Page 102 Chapter 9 Abstract Data Types and Algorithms Page 102

  20. Data Structure #5: The Queue 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. Example: Chapter 9 Abstract Data Types and Algorithms Page 103 Chapter 9 Abstract Data Types and Algorithms Page 103

  21. Comparison: Queue Implementations Using an array Using a linked list void Insert(nodePtr &ListFront, nodePtr &ListRear, int item) { nodePtrptr = 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) { intval = -1; if (ListFront != NULL) { val = ListFront->value; ListFront = ListFront->next; } if (ListFront == NULL) ListRear = NULL; 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) { intval = -1; if (Front > -1) { val = List[Front]; if (Front == Rear) Front = Rear = -1; else Front = (Front+1)%50; } return val; } Chapter 9 Abstract Data Types and Algorithms Page 104 Chapter 9 Abstract Data Types and Algorithms Page 104

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

  23. 8 3 14 1 5 10 19 7 12 16 23 17 Data Structure #6: The Binary 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 Implementation: struct node; typedef node *nodePtr; struct node { int value; nodePtr left; nodePtr right; }; nodePtr Tree; • Example: Binary Insertion Tree • Each 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 value Chapter 9 Abstract Data Types and Algorithms Page 106 Chapter 9 Abstract Data Types and Algorithms Page 106

  24. Recursive Insertion into a Binary Insertion Tree 8 3 14 1 5 10 19 7 12 16 23 17 void Bin_Insert(nodePtr &Tree, int item) { if (Tree == NULL) { nodePtrptr = new node; ptr->value = item; ptr->left = NULL; ptr->right = NULL; } else if (item < Tree->value) Bin_Insert(Tree->left, item); else Bin_Insert(Tree->right, item); } Example: Where will a new node containing the integer 11 be inserted? 11 Chapter 9 Abstract Data Types and Algorithms Page 107 Chapter 9 Abstract Data Types and Algorithms Page 107

  25. Recursive Traversal of a Binary Insertion Tree 8 3 14 1 5 10 19 7 12 16 23 17 void Inorder(nodePtr Tree) { if (Tree != NULL) { Inorder(Tree->left); cout << Tree->value << endl; Inorder(Tree->right); } } 1 3 5 7 8 10 12 14 Example: Apply Inorder to this binary insertion tree: 16 17 19 23 Chapter 9 Abstract Data Types and Algorithms Page 108 Chapter 9 Abstract Data Types and Algorithms Page 108

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

More Related