Sorted Lists CS 302 - Data Structures Sections 4.1, 4.2 & 4.3.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
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
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
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.
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4.
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.
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.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Arrays Linear search Binary search small arrays
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.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
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.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
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.
Binary Search Trees. What is a binary tree? Property 1: each node can have up to two successor nodes.
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.
Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.
C++ Classes and Data Structures Jeffrey S. Childs
Recursion CS 3358 – Data Structures. Big Picture Our objective is to write a recursive program and convince ourselves it is correct with the minimum amount.
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
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 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
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.
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 16: Searching, Sorting, and the vector Type.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
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
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
CSI 1340 Introduction to Computer Science II
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
Recursion.
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Presentation transcript:

Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3

Sorted List Implementations Array-based Linked-list-based

Array-based Implementation template class SortedType { public: void MakeEmpty(); bool IsFull() const; int LengthIs() const; void RetrieveItem(ItemType&, bool&); void InsertItem(ItemType); void DeleteItem(ItemType); void ResetList(); bool IsLastItem(); void GetNextItem(ItemType&); private: int length; ItemType info[MAX_ITEMS]; int currentPos; };

InsertItem InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List has been initialized, (2) List is not full, (3) item is not in list (4) List is sorted by key member. Postconditions: (1) item is in list, (2) List is still sorted.

Array-based Implementation template void SortedType ::InsertItem(ItemType item) { int location = 0; bool found; found = false; while( (location < length) && !found) { if (item < info[location]) found = true; else location++; } (cont) O(N)

Array-based Implementation for (int index = length; index > location; index--) info[index] = info[index - 1]; info[location] = item; length++; } O(N) Total time: O(N) O(1)

DeleteItem DeleteItem(ItemType item) Function: Deletes the element whose key matches item's key Preconditions: (1) List has been initialized, (2) Key member of item has been initialized, (3) There is only one element in list which has a key matching item's key, (4) List is sorted by key member. Postconditions: (1) No element in list has a key matching item's key, (2) List is still sorted.

Array-based Implementation template void SortedType ::DeleteItem(ItemType item) { int location = 0; while (item != info[location]) location++; for (int index = location + 1; index < length; index++) info[index - 1] = info[index]; length--; } O(N) Total time: O(N)

RetrieveItem (ItemType& item, boolean& found) Function: Retrieves list element whose key matches item's key (if present). Preconditions: (1) List has been initialized, (2) Key member of item has been initialized. Postconditions: (1) If there is an element someItem whose key matches item's key, then found=true and item is a copy of someItem; otherwise, found=false and item is unchanged, (2) List is unchanged.

Might not have to search the whole list! Naive approach: use Linear Search Algorithm item is not in the list retrieve Sarah retrieve George item is in the list

Improved RetrieveItem() template void SortedType ::RetrieveItem (ItemType& item, bool& found) { int location = 0; found = false; while ( (location < length) && !found) { if ( item > info[location]) { location++; else if(item < info[location]) location = length; // to break out of the loop… else { found = true; item = info[location]; } Still O(N) …

Binary Search Algorithm Split the current search area in half, and if the item is not found there, then search the appropriate half.

- Search for 24:

Binary Search Algorithm (cont.) template void SortedType :: RetrieveItem(ItemType& item, bool& found) { int midPoint; int first = 0; int last = length - 1; found = false; while( (first <= last) && !found) { midPoint = (first + last) / 2; if (item < info[midPoint]) last = midPoint - 1; else if(item > info[midPoint]) first = midPoint + 1; else { found = true; item = info[midPoint]; } O(logN)

Binary Search Efficiency (1) Number of iterations: –For a list of 11 elements, it never iterates more than 4 times (e.g., approximately log 2 11 times). –Linear Search can iterate up to 11 times. Number of Iterations List LengthLinear Search (average) Binary Search , ,

Binary Search Efficiency (contd) (2) Number of computations per iteration: –Binary search does more work per iteration than Linear Search while( (first <= last) && !found) { midPoint = (first + last) / 2; if (item < info[midPoint]) last = midPoint - 1; else if(item > info[midPoint]) first = midPoint + 1; else { found = true; item = info[midPoint]; } while ( (location < length) && !found) { if ( item > info[location]) { location++; else if(item < info[location]) location = length; // to break out of the loop… else { found = true; item = info[location]; } Linear search iterationsBinary search iterations

Is Binary Search more efficient? Overall, it can be shown that: –If the number of list elements is small (typically, under 20), then Linear Search is faster. –If the number of list elements is large, then Binary Search is faster.

List Implementations Big-O Comparison of List Operations OperationUnsortedSorted MakeEmptyO(1) LengthIsO(1) IsFullO(1) ResetListO(1) GetNextItemO(1) RetrieveItemO(N)O(log N) InsertItemO(1)O(N) DeleteItemO(N)

Example Suppose we have a million elements in an sorted list; which algorithm would be faster? (1) A binary search on a 500-MHz computer or (2) A linear search on a 5-GHz computer

Example (contd) Assumptions: (1) Each iteration of a linear search will be twice as fast as each iteration of a binary search on the same computer. (2) Each instruction on the 5-GHz computer is 10 times faster than each instruction on the 500-MHz computer.

Example (contd) Consider number of iterations first: Binary Search Linear Search log 2 (1,000,000) ~ 20 1,000,000 iterations (worst-case) (worst-case) or 500,000 (average-case) Binary search will be 500,000/20 = 25,000 faster than linear search.

Example (contd) Assuming same computers and using assumption (1): –Binary search would be 25,000/2 = 12,500 faster!

Example (contd) Assuming different computers and using both assumptions (1) and (2): –Binary search will be 25,000/20 = 1250 times faster on the 500-MHz computer than linear search on the 5-GHz computer!

Linked-list-based Implementation template struct NodeType; template class SortedType { public: SortedType(); ~SortedType(); void MakeEmpty(); bool IsFull() const; int LengthIs() const; void RetrieveItem(ItemType&, bool&); void InsertItem(ItemType); void DeleteItem(ItemType); void ResetList(); bool IsLastItem() const; void GetNextItem(ItemType&); private: int length; NodeType * listData; NodeType * currentPos; };

RetrieveItem (ItemType& item, boolean& found) Function: Retrieves list element whose key matches item's key (if present). Preconditions: (1) List has been initialized, (2) Key member of item has been initialized. Postconditions: (1) If there is an element someItem whose key matches item's key, then found=true and item is a copy of someItem; otherwise, found=false and item is unchanged, (2) List is unchanged.

RetrieveItem Could use linear search O(N) time

RetrieveItem (cont.) template void SortedType ::RetrieveItem(ItemType& item, bool& found) { NodeType * location; location = listData; found = false; while( (location != NULL) && !found) { if (location info < item) location = location next; else if (location info == item) { found = true; item = location info; } else location = NULL; // to break out of the loop … } O(N)

What about Binary Search? Not efficient any more! –Cannot find the middle element in O(1) time.

InsertItem InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List has been initialized, (2) List is not full, (3) item is not in list (4) List is sorted by key member. Postconditions: (1) item is in list, (2) List is still sorted.

InsertItem

InsertItem (cont.) Can we compare one item ahead? –Yes, but we need to check for special cases … In general, we must keep track of the previous pointer, as well as the current pointer.

InsertItem (cont.) prevLoc = location location = location next

Insert at the beginning of the list newNode next= location; listData=newNode; Case 1

Insert between first and last elements newNode next=location; prevLoc next = newNode; Case 2

Insert at the end of the list newNode next=location; prevLoc next = newNode; Case 3

Insert into an empty list newNode next=location; listData=newNode; Case 4

newNode next= location; listData=newNode; newNode next=location; prevLoc next = newNode; newNode next=location; prevLoc next = newNode; newNode next=location; listData=newNode; (1) (2) (3) (4)

InsertItem (cont.) template void SortedType ::InsertItem(ItemType newItem) { NodeType * newNode; NodeType * predLoc; NodeType * location; bool found; found = false; location = listData; predLoc = NULL; while( location != NULL && !found) { if (location info < newItem) { predLoc = location; location = location next; } else found = true; } O(N) O(1)

InsertItem (cont.) newNode = new NodeType ; newNode info = newItem; if (predLoc == NULL) { newNode next = listData; cases (1) and (4) listData = newNode; } else { newNode next = location; predLoc next = newNode; cases (2) and (3) } length++; } O(1)

DeleteItem DeleteItem(ItemType item) Function: Deletes the element whose key matches item's key Preconditions: (1) List has been initialized, (2) Key member of item has been initialized, (3) There is only one element in list which has a key matching item's key, (4) List is sorted by key member. Postconditions: (1) No element in list has a key matching item's key, (2) List is still sorted.

DeleteItem The DeleteItem we wrote for unsorted lists would work for sorted lists too! Another possibility is to write a new DeleteItem based on several cases (see textbook)

Same as in the UnsortedList class... Other SortedList functions

Sorted List Implementations Big-O Comparison of Sorted List Operations OperationArray Implementation Linked Implementation Class constructorO(1) DestructorO(1)O(N) MakeEmptyO(1)O(N) IsFullO(1) LengthIsO(1) ResetListO(1) GetNextItemO(1) RetrieveItemO(logN)O(N) InsertItemO(N) DeleteItemO(N)

Exercise: Write a client function that splits a sorted list into two sorted lists using the following specification. SplitLists (SortedType list, ItemType item, SortedType& list1, SortedType& list 2) Function: Divides list into two lists according to the key of item. Preconditions: list has been initialized and is not empty. Postconditions: list1 contains all the items of list whose keys are less than or equal to items key. list2 contains all the items of list whose keys are greater than items key.

void SplitLists(const SortedType& list, ItemType item, SortedType& list1, SortedType& list2) { ItemType listItem; list1.MakeEmpty(); list2.MakeEmpty(); list.ResetList(); while (!list.IsLastItem()) { list.GetNextItem(listItem); if(listItem > item) list2.InsertItem(listItem); else list1.InsertItem(listItem); } What is the running time using big-O? O(N 2 )

Exercise: Write a client function that takes two lists (unsorted or sorted) and returns a Boolean indicating whether the second list is a sublist of the first. (i.e., the first list contains all the elements in the second list but it might contain other elements too).

bool IsSubList (SortedType list1, SortedType list2) { ItemType item; bool found=true; list2.ResetList(); while ( !list2.IsLastItem() && found) { list2.GetNextItem (item); list1.RetrieveItem (item, found); } return found; } What is the running time using big-O? O(NlogN) assuming array-based O(N 2 ) assuming array-based