sorted list cs3240. 2 goals describe the abstract data type sorted list from three perspectives...
TRANSCRIPT
![Page 1: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/1.jpg)
Sorted List
CS3240
![Page 2: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/2.jpg)
2
Goals
• Describe the Abstract Data Type Sorted List from three perspectives
• Implement the following Sorted List operations using an array-based implementation
– Create and destroy a list
– Determine whether the list is full
– Insert an element
– Retrieve an element
– Delete an element
![Page 3: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/3.jpg)
3
Goals
• Implement the list operations outlined above using a linked implementation
• Implement the binary search algorithm
• Compare the two implementations of the ADT Sorted List in terms of Big-O approximations
• Compare the implementations of the Unsorted List ADT and the Sorted List ADT in terms of Big-O Analysis
• Distinguish between bounded and unbounded ADTs at the logical and implementation levels
• Identify and apply the phases of the object-oriented methodology
![Page 4: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/4.jpg)
4
ADT Sorted List
Remember the difference between an unsorted list and a sorted list?
Remember the definition of a key?
If the list is a sorted list
of names, what would be the key?
of bank balances, what would be the key?
of grades, what would be the key?
![Page 5: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/5.jpg)
5
ADT Unsorted List Operations
Transformers – MakeEmpty – InsertItem – DeleteItem
Observers – IsFull– GetLength
– RetrieveItem
Iterators – ResetList – GetNextItem
change state
observe state
process all
![Page 6: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/6.jpg)
6
Which member function specifications and implementations must change to ensure that any instance of the Sorted List ADT remains sorted at all times?
– InsertItem
– DeleteItem
ADT Sorted List
What about the othertransformer MakeEmtpy?
![Page 7: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/7.jpg)
7
Array Implementation
What do you have to do to insert Clair into the following list?
Anne
Betty
Mary
Susan
![Page 8: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/8.jpg)
Array Implementation
• Find proper location for the new element in the sorted list
• Create space for the new element by moving down all the list elements that will follow it
• Put the new element in the list
• Increment length
![Page 9: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/9.jpg)
9
Array ImplementationInsertItem
Initialize location to position of first item
Set moreToSearch to (have not examined Info(last))
while moreToSearch
switch (item.ComparedTo(Info(location)))
case LESS : Set moreToSearch to false
case EQUAL : // Cannot happen
case GREATER :
Set location to Next(location)
Set moreToSearch to (have not examined Info(last))
for index going from length DOWNTO location + 1
Set Info(index ) to Info(index-1)
Set Info(location) to item
Increment length
Why can't EQUAL happen?
![Page 10: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/10.jpg)
10
Array Implementation
What were the conversions from notation to array-based code?
info(last)
info(location)
Next(location)
![Page 11: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/11.jpg)
11
Array ImplementationDeleteItem
Initialize location to position of first itemSet found to falsewhile NOT foundswitch (item.ComparedTo(Info(location)))
case GREATER : Set location to Next(location)case LESS : // Cannot happencase EQUAL : Set found to true
for index going from location +1 TO length -1Set Info(index - 1) to Info(index)
Decrement length
Why can't LESS happen?
![Page 12: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/12.jpg)
12
Array Implementation
Can we improve searching in a sorted list?With the Unsorted List ADT we examined each list element beginning with info[ 0 ], until we found a matching key or we examined all the elements
![Page 13: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/13.jpg)
13
Binary Search Algorithm
Examine the element in the middle of the array– Match item?
• Stop searching
– Middle element too small?• Search second half of array
– Middle element too large?• Search first half of array Is it the sought item?
Repeat the process in half that should be examined next
Stop when item is found or when there is nowhere else to look
![Page 14: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/14.jpg)
void SortedType::RetrieveItem ( ItemType& item, bool& found ) // Pre: Key member of item is initialized.// Post: If found, item’s key matches an element’s key and a copy // of element has been stored in item; otherwise, item is // unchanged.
{ int midPoint;int first = 0;
int last = length - 1 bool moreToSearch = ( first <= last );
found = false;while ( moreToSearch && !found ){ midPoint = ( first + last ) / 2; switch ( item.ComparedTo(info[midPoint]) )
{ case LESS : . . . // LOOK IN FIRST HALF NEXT case GREATER : . . . // LOOK IN SECOND HALF NEXT case EQUAL : . . . // ITEM HAS BEEN FOUND
} }}
Binary Search Algorithm
![Page 15: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/15.jpg)
15
Trace of Binary Search
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
item = 45
first midPoint last
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
first midPoint last
LESS last = midPoint - 1
GREATER first = midPoint + 1
![Page 16: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/16.jpg)
16
Trace continued
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
item = 45
first, midPoint, last
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
first, last midPoint
LESS last = midPoint - 1
GREATER first = midPoint + 1
![Page 17: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/17.jpg)
Array-Based Big-O Comparison
OPERATION UnsortedList SortedList
RetrieveItem O(N) O(N) linear search O(log2N) binary search
InsertItemFind O(1) O(N) searchPut O(1) O(N) moving downCombined O(1) O(N)
DeleteItemFind O(N) O(N) searchPut O(1) swap O(N) moving upCombined O(N) O(N)
![Page 18: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/18.jpg)
18
Dynamically Allocated Arrays{public:SortedType(int max); // max is maximum list sizeSortedType(); // Default size is 500// Rest of the prototypes go here
private: int length; int maxList; // Maximum number of list items
ItemType* info; // Pointer to dynamically // allocated memory int currentPos;
};
What does this structure look like?
![Page 19: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/19.jpg)
19
Dynamically Allocated Arrays
Howdoyou
accessthe
arrayelements
?
![Page 20: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/20.jpg)
20
Linked Implementation
Does replacing algorithm terms with linked code work for the linked implementation of searching?
![Page 21: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/21.jpg)
21
Linked Implementation
What about passing spot where item would be if there?
![Page 22: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/22.jpg)
22
Linked Implementation
Is Inserting as easy? Let's see
Set location to listDataSet moreToSearch to (location != NULL)while moreToSearch
switch (item.ComparedTo(location->info)) case GREATER :
Set location to location->nextSet moreToSearch to (location != NULL)
case LESS : Set moreToSearch to false
Seethe
problem?
![Page 23: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/23.jpg)
23
Linked Implementation
We need a trailing pointer
![Page 24: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/24.jpg)
24
Inserting ‘S’ into a Sorted List
‘C’ ‘L’ ‘X’
Private data:
length 3
listData
currentPos ?
predLoc location
moreToSearch
![Page 25: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/25.jpg)
25
Finding proper position for ‘S’
‘C’ ‘L’ ‘X’
Private data:
length 3
listData
currentPos ?
predLoc location
NULL
moreToSearch true
![Page 26: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/26.jpg)
26
Finding proper position for ‘S’
‘C’ ‘L’ ‘X’
Private data:
length 3
listData
currentPos ?
predLoc location
moreToSearch true
![Page 27: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/27.jpg)
27
Finding Proper Position for ‘S’
‘C’ ‘L’ ‘X’
Private data:
length 3
listData
currentPos ?
predLoc location
moreToSearch false
![Page 28: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/28.jpg)
28
Inserting ‘S’ into Proper Position
‘C’ ‘L’ ‘X’
Private data:
length 4
listData
currentPos
predLoc location
moreToSearch false
‘S’
![Page 29: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/29.jpg)
29
Linked Implementation
Does DeleteItem have to be changed?
![Page 30: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/30.jpg)
30
Big-O Comparison
Which array-based operations are O(1)?
Which linked operations are O(1)?
Which array-based operations are O(N)?
Which linked operations are O(N)?
Can you say which implementation is better?
Which sorted operations differ in Big-O from unsorted operations?
![Page 31: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/31.jpg)
31
Bounded and Unbounded ADTs
Bounded ADT
An ADT for which there is a logical limit on the number of items in the structure
Unbounded ADT
An ADT for which there is no logical limit on the number of items in the structure
How do you relate the logical limit to the physical limit?
![Page 32: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/32.jpg)
32
Object-Oriented Design Methodology
Object-oriented design decomposes a problem into classes
Four stages to the decomposition process
– Brainstorming
– Filtering
– Scenarios
– Responsibility algorithms
![Page 33: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/33.jpg)
33
Object-Oriented Design Methodology
BrainstormingA group problem-solving technique that involves the spontaneous contribution of ideas from all members of the group
– All ideas are potential good ideas– Think fast and furiously first, and ponder later– A little humor can be a powerful force
Brainstorming is designed to produce a list of candidate classes
![Page 34: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/34.jpg)
34
Object-Oriented Design Methodology
Filtering
Determine which are the core classes in the problem solution
– There may be two classes in the list that have many common attributes and behaviors
– There may be classes that really don’t belong in the problem solution
![Page 35: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/35.jpg)
35
Object-Oriented Design Methodology
ScenariosSequences of steps that describe an interaction between a client and an application or program
– Simulate class interactions– Ask “What if?” questions– Assign responsibilities to each class
– There are two types of responsibilities• What a class must know about itself (knowledge) • What a class must be able to do (behavior)
Use caseA collection of scenarios related to a common goal
![Page 36: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/36.jpg)
36
Object-Oriented Design Methodology
Roleplaying
![Page 37: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/37.jpg)
37
Object-Oriented Design Methodology
Responsibility AlgorithmsThe algorithms must be written for the responsibilities
– Knowledge responsibilities usually just return the contents of one of an object’s variables
– Action responsibilities are a little more complicated, often involving calculations
Responsibilities algorithms are often decomposed using top-down design
![Page 38: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/38.jpg)
38
Relationships Between Classes
Containment– “part-of” – An address class may be part of the definition of a
student class
Inheritance– Classes can inherit data and behavior from other
classes– “is-a”
![Page 39: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/39.jpg)
39
Computer Example
Let’s work through this problem-solving process by creating an address listBrainstorming and filtering
– Circling the nouns and underlining the verbs is a good way to begin
![Page 40: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/40.jpg)
40
Computer Example
First pass at a list of classeslistnametelephone numberemail addresslistorder nameslistscrapspapercards
Filtered List
list, name, telephone number, email address
![Page 41: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/41.jpg)
41
CRC Cards
Can you think of any other useful responsibilities?
![Page 42: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/42.jpg)
42
CRC Cards
Can you think of any other useful responsibilities?
![Page 43: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/43.jpg)
43
CRC Cards
How is this class different from Name and Person?
![Page 44: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/44.jpg)
44
Responsibility Algorithms
Person Class
Initialize
name.initialize()Write "Enter phone number; press return."Get telephone numberWrite "Enter email address; press return."Get email address
name.print() Write "Telephone number: " + telephoneNumberWrite "Email address: " + emailAddress
Tells name to initialize itself
Tells name to print itself
![Page 45: Sorted List CS3240. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an](https://reader035.vdocuments.us/reader035/viewer/2022062222/5697bf841a28abf838c86eb9/html5/thumbnails/45.jpg)
45
Responsibility Algorithms
Name Class
Initialize
"Enter the first name; press return."Read firstName"Enter the last name; press return."Read lastName
Print "First name: " + firstNamePrint "Last name: " + lastName