Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.
Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
6-1 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer problem-solving process and relate it to Polya’s.
Problem Solving and Algorithm Design
Chapter 6 Problem Solving and Algorithm Design. 6-2 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer.
Unsorted Lists CS 308 – Data Structures. What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Chapter 4 ADT Sorted List.
Sorted Lists CS Data Structures. Sorted List Specification (partial) InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List.
Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures.
Algorithms and Problem Solving-1 Algorithms and Problem Solving Mainly based on Chapter 6: “Problem Solving and Algorithm Design” from the Book: “Computer.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Algorithms and Problem Solving
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Nell Dale Chapter 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 6 Problem Solving and Algorithm Design Nell Dale John Lewis.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
Data Structures Using C++ 2E
Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design.
Chapter 4 ADT Sorted List.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
CSC 211 Data Structures Lecture 13
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
1 Nell Dale Lecture 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta.
CSI 1340 Introduction to Computer Science II Chapter 3 ADTs Unsorted List and Sorted List.
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Algorithms and Problem Solving
Chapter 13 Applied Arrays: Lists and Strings
C++ Plus Data Structures ADTs Unsorted List and Sorted List
TA: Nouf Al-Harbi Data Structures LAB 3 TA: Nouf Al-Harbi
C++ Plus Data Structures
Object-Oriented Design
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 16 Linked Structures
Algorithms and Problem Solving
CSI 1340 Introduction to Computer Science II
Chapter 13 Applied Arrays: Lists and Strings
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Presentation transcript:

Chapter 4 ADT Sorted List

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

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

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?

5 ADT Unsorted List Operations Transformers –MakeEmpty –InsertItem –DeleteItem Observers –IsFull –GetLength –RetrieveItem Iterators –ResetList –GetNextItem change state observe state process all

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 other transformer MakeEmtpy?

7 Array Implementation What do you have to do to insert Clair into the following list? Anne Betty Mary Susan

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

9 Array Implementation InsertItem 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?

10 Array Implementation What were the conversions from notation to array-based code? info(last) info(location) Next(location)

11 Array Implementation DeleteItem Initialize location to position of first item Set found to false while NOT found switch (item.ComparedTo(Info(location))) case GREATER : Set location to Next(location) case LESS: // Cannot happen case EQUAL : Set found to true for index going from location +1 TO length -1 Set Info(index - 1) to Info(index) Decrement length Why can't LESS happen?

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

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

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; intlast = 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

15 Trace of Binary Search info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 first midPoint last info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last LESS last = midPoint - 1 GREATERfirst = midPoint + 1

16 Trace continued info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 first, midPoint, last info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last midPoint LESS last = midPoint - 1GREATERfirst = midPoint + 1

Array-Based Big-O Comparison OPERATION UnsortedList SortedList RetrieveItem O(N) O(N) linear search O(log 2 N) binary search InsertItem Find O(1) O(N) search Put O(1) O(N) moving down Combined O(1) O(N) DeleteItem Find O(N) O(N) search Put O(1) swap O(N) moving up Combined O(N) O(N)

18 Dynamically Allocated Arrays { public: SortedType(int max); // max is maximum list size SortedType(); // 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?

19 Dynamically Allocated Arrays How do you access the array elements ?

20 Linked Implementation Does replacing algorithm terms with linked code work for the linked implementation of searching?

21 Linked Implementation What about passing spot where item would be if there?

22 Linked Implementation Is Inserting as easy? Let's see Set location to listData Set moreToSearch to (location != NULL) while moreToSearch switch (item.ComparedTo(location->info)) case GREATER: Set location to location->next Set moreToSearch to (location != NULL) case LESS: Set moreToSearch to false See the problem ?

23 Linked Implementation We need a trailing pointer

24 Inserting ‘S’ into a Sorted List ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location moreToSearch

25 Finding proper position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location NULL moreToSearch true

26 Finding proper position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location moreToSearch true

27 Finding Proper Position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location moreToSearch false

28 Inserting ‘S’ into Proper Position ‘C’ ‘L’ ‘X’ Private data: length 4 listData currentPos predLoc location moreToSearch false ‘S’

29 Linked Implementation Does DeleteItem have to be changed?

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?

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?

32 Object-Oriented Design Methodology Object-oriented design decomposes a problem into classes Four stages to the decomposition process –Brainstorming –Filtering –Scenarios –Responsibility algorithms

33 Object-Oriented Design Methodology Brainstorming A 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

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

35 Object-Oriented Design Methodology Scenarios Sequences 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 case A collection of scenarios related to a common goal

36 Object-Oriented Design Methodology Role playing

37 Object-Oriented Design Methodology Responsibility Algorithms The 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

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”

39 Computer Example Let’s work through this problem-solving process by creating an address list Brainstorming and filtering –Circling the nouns and underlining the verbs is a good way to begin

40 Computer Example First pass at a list of classes list name telephone number address list order names list scraps paper cards Filtered List list, name, telephone number, address

41 CRC Cards Can you think of any other useful responsibilities?

42 CRC Cards Can you think of any other useful responsibilities?

43 CRC Cards How is this class different from Name and Person?

44 Responsibility Algorithms Person Class Initialize name.initialize() Write "Enter phone number; press return." Get telephone number Write "Enter address; press return." Get address Print name.print() Write "Telephone number: " + telephoneNumber Write " address: " + Address Tells name to initialize itself Tells name to print itself

45 Responsibility Algorithms Name Class Initialize "Enter the first name; press return." Read firstName "Enter the last name; press return." Read lastName Print Print "First name: " + firstName Print "Last name: " + lastName