unsorted lists
DESCRIPTION
Unsorted Lists. CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5. Unsorted list. A list in which data items are placed in no particular order. Sorted list. A list in which data items are placed in a particular order. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/1.jpg)
Unsorted Lists
CS 302 – Data StructuresSections 3.1, 3.2, 3.4 & 3.5
![Page 2: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/2.jpg)
Unsorted list• A list in which data items are placed in no
particular order.
Sorted list• A list in which data items are placed in a
particular order.
• Key: a member of the class whose value is used to determine the order of the items in the list.
![Page 3: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/3.jpg)
![Page 4: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/4.jpg)
Unsorted List ImplementationsArray-based
Linked-list-based
![Page 5: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/5.jpg)
Array-based Implementation
template<class ItemType>class UnsortedType { 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;};
currentPos
![Page 6: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/6.jpg)
Length: length of the list (different from array size!)
![Page 7: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/7.jpg)
Array-based Implementation
template<class ItemType>void UnsortedType<ItemType>::MakeEmpty(){
length = 0;}
template<class ItemType>bool UnsortedType<ItemType>::IsFull() const{
return (length == MAX_ITEMS);}
(cont.)
O(1)
O(1)
![Page 8: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/8.jpg)
Array-based Implementation
template<class ItemType>
int UnsortedType<ItemType>::LengthIs() const{
return length;}
(cont.)
O(1)
![Page 9: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/9.jpg)
• 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 (ItemType& item, boolean& found)
![Page 10: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/10.jpg)
Item is in the list Item is NOT in the list
Retrieve Judy
![Page 11: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/11.jpg)
Array-based Implementation
template<class ItemType>void UnsortedType<ItemType>::RetrieveItem (ItemType& item,
bool& found){ int location = 0; found = false;
while( (location < length) && !found)
if (item == info[location]) {
found = true; item = info[location]; } else location++;}
(cont.)
O(N)
Who should overload “==“ ?
![Page 12: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/12.jpg)
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 (i.e., no duplicates)
• Postconditions: item is in list.
![Page 13: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/13.jpg)
InsertItem (ItemType item) (cont’d)
Insert George
![Page 14: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/14.jpg)
Array-based Implementation
template<class ItemType>void UnsortedType<ItemType>::InsertItem (ItemType
item){ info[length] = item; length++;}
(cont.)
O(1)
![Page 15: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/15.jpg)
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.
• Postconditions: No element in list has a key matching item's key.
![Page 16: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/16.jpg)
easy
possible better
![Page 17: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/17.jpg)
Array-based Implementation (cont.)
template<class ItemType>void UnsortedType<ItemType>::DeleteItem(ItemType item){
int location = 0;
while(item != info[location]) location++;
info[location] = info[length - 1]; length--;}
O(N)O(1)
O(N)
![Page 18: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/18.jpg)
Traversing the List
• ResetList
• GetNextItem
• IsLastItem
currentPos
![Page 19: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/19.jpg)
ResetList
• Function: Initializes currentPos for an iteration through the list.
• Preconditions: List has been initialized
• Postconditions: Current position is prior to first element in list.
![Page 20: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/20.jpg)
![Page 21: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/21.jpg)
Array-based Implementation (cont.)
template<class ItemType>void UnsortedType<ItemType>::ResetList(){ currentPos = -1;}
O(1)
currentPos
![Page 22: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/22.jpg)
GetNextItem (ItemType& item)
• Function: Gets the next element in list.
• Preconditions: (1) List has been initialized,
(2) Current position is not the last item.
• Postconditions: (1) Current position is updated to next position, (2) item is a copy of element at current position.
![Page 23: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/23.jpg)
Array-based Implementation (cont.)
template<class ItemType>void UnsortedType<ItemType>::GetNextItem (ItemType& item){ currentPos++; item = info[currentPos];}
O(1)
![Page 24: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/24.jpg)
Array-based Implementation (cont.)
template<class ItemType>bool UnsortedType<ItemType>::IsLastItem (){ return (currentPos == length-1);}
O(1)
![Page 25: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/25.jpg)
Warning
• When traversing the list using GetNextItem, the list should not be modified (i.e., add or delete elements)
• This might modify the value of currentPos.
![Page 26: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/26.jpg)
Linked-list-based Implementationtemplate <class ItemType>struct NodeType; template<class ItemType>class UnsortedType { public: UnsortedType(); ~UnsortedType(); 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<ItemType>* listData; NodeType<ItemType>* currentPos; };
![Page 27: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/27.jpg)
• 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 (ItemType& item, boolean& found)
![Page 28: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/28.jpg)
RetrieveItem
![Page 29: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/29.jpg)
RetrieveItem (cont.)template<class ItemType>void UnsortedType<ItemType>::RetrieveItemRetrieveItem
(ItemType& item, bool& found)
{
NodeType<ItemType>* location;
location = listData; found = false;
while( (location != NULL) && !found) {
if(item == location->info) { found = true; item = location->info; }
else location = location->next; }}
O(N)
![Page 30: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/30.jpg)
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 (i.e., no duplicates)
• Postconditions: item is in list.
![Page 31: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/31.jpg)
InsertItem
• Insert the item at the beginning of the list
![Page 32: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/32.jpg)
InsertItem (cont.)template <class ItemType>void UnsortedType<ItemType>::InsertItem (ItemType newItem){ NodeType<ItemType>* location;
location = new NodeType<ItemType>; location->info = newItem; location->next = listData; listData = location; length++;}
O(1)
![Page 33: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/33.jpg)
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.
• Postconditions: No element in list has a key matching item's key.
![Page 34: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/34.jpg)
DeleteItem
• Find the item to be deleted.
• In order to delete it, we must change the “next” pointer in the previous node!
![Page 35: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/35.jpg)
DeleteItem (cont.)• Idea: compare one item ahead: i.e., (location->next)->info)
• Works efficiently since the item to be deleted is on the list (i.e., precondition).
Special case: deleting the first node!
![Page 36: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/36.jpg)
Function DeleteItem (cont.)template <class ItemType>void UnsortedType<ItemType>::DeleteItemDeleteItem(ItemType item){ NodeType<ItemType>* location = listData; NodeType<ItemType>* tempLocation;
if(item == listData->info) { tempLocation = listData; // special case listData = listData->next; } else {
while(!(item == (location->next)->info)) location = location->next;
// delete node at location->next
tempLocation=location->next; location->next = tempLocation->next; }
delete tempLocation; length--;}
O(1)
O(N)
O(1)
O(1)
Overall: O(N)
![Page 37: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/37.jpg)
Other UnsortedList functions template<class ItemType>UnsortedType<ItemType>::UnsortedType(){
length = 0; listData = NULL;}
template<class ItemType>void UnsortedType<ItemType>::MakeEmpty(){
NodeType<ItemType>* tempPtr;
while(listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; }
length=0;}
O(1)
O(N)
![Page 38: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/38.jpg)
Other UnsortedList functions (cont.)template<class ItemType>UnsortedType<ItemType>::~UnsortedType(){
MakeEmpty();}
template<class ItemType>bool UnsortedType<ItemType>::IsFull() const{
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>; if(ptr == NULL) return true; else { delete ptr; return false; }}
O(N)
O(1)
![Page 39: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/39.jpg)
template<class ItemType>int UnsortedType<ItemType>::LengthIs() const{ return length;}
template<class ItemType>int UnsortedType<ItemType>::ResetList(){ currentPos = listData;}
Other UnsortedList functions (cont.)
O(1)
O(1)
![Page 40: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/40.jpg)
template<class ItemType>void UnsortedType<ItemType>::GetNextItem(ItemType& item){ item = currentPos->info; currentPos = currentPos->next;}
template<class ItemType>bool UnsortedType<ItemType>::IsLastItem() const{ return(currentPos == NULL);}
Other UnsortedList functions (cont.)
O(1)
O(1)
![Page 41: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/41.jpg)
Comparing unsorted list implementations
Big-O Comparison of Unsorted List Operations
Operation Array Implementation
Linked Implementation
Constructor O(1) O(1)
Destructor O(1) O(N)
MakeEmpty O(1) O(N)
IsFull O(1) O(1)
LengthIs O(1) O(1)
ResetList O(1) O(1)
GetNextItem O(1) O(1)
RetrieveItem O(N) O(N)
InsertItem O(1) O(1)
DeleteItem O(N) O(N)
![Page 42: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/42.jpg)
Exercise: Write a client function that splits an unsorted list into two unsorted lists using the following specification.
SplitLists (UnsortedType list, ItemType item, UnsortedType& list1, UnsortedType& 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 item’s key; list2 contains all the items of list whose keys are greater than item’s key; list remains unchanged.
![Page 43: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/43.jpg)
void SplitLists(const UnsortedType& list, ItemType item, UnsortedType& list1, UnsortedType& 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 time complexity using big-O?
O(N)
![Page 44: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/44.jpg)
Exercise: Write a client function that merges two unsorted lists into another unsorted list using the following specification.
MergeLists (UnsortedType list1, UnsortedType list2, UnsortedType& list)
Function: Merge list1 and list2 into another list.
Preconditions: list1 and list2 have been initialized.
Postconditions: list contains all the items of list1 and list2; list does not contain duplicates.
![Page 45: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/45.jpg)
void MergeLists(UnsortedType list1, UnsortedType list2, UnsortedType&
list){ ItemType item; bool found;
list.MakeEmpty();
list1.ResetList(); while(!list1.IsLastItem()) { list1.GetNextItem(item); list.InsertItem(item); }
O(N1)
![Page 46: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/46.jpg)
list2.ResetList(); while (!list2.IsLastItem() && (!list.IsFull())) {
list2.GetNextItem(item); list.RetrieveItem(item, found);
if(!found) list.InsertItem(item) } if(list.IsFull() && (!list2.IsLastItem())) cout << “Not enough space to merge the lists” << endl; }
O(N1+N2(N1+N2))=O(N1N2+(N2)2)
O(N2(N1+N2))
equivalent O(max(N1, N2(N1+N2)))
![Page 47: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/47.jpg)
list2.ResetList(); while (!list2.IsLastItem() && (!list.IsFull())) {
list2.GetNextItem(item); list1.RetrieveItem(item, found);
if(!found) list.InsertItem(item) } if(list.IsFull() && (!list2.IsLastItem())) cout << “Not enough space to merge the lists” << endl; }
O(N1+N2N1)=O(N1N2)
more efficient!
O(N2N1)
equivalent to O(max(N1, N2N1))
![Page 48: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/48.jpg)
Detailed Analysis
O(N1N2+(N2)2) O(N1N2)
1. N1=N2=O(N) O(2N2)=O(N2) O(N2)
Solution 1 Solution 2
![Page 49: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/49.jpg)
Detailed Analysis (cont’d)
O(N1N2+(N2)2) O(N1N2)
2. N1=O(1)
N2=O(N) O(cN+N2)=O(N2) O(cN)=O(N)
Solution 1 Solution 2
![Page 50: Unsorted Lists](https://reader033.vdocuments.us/reader033/viewer/2022061501/568136df550346895d9e7a74/html5/thumbnails/50.jpg)
Detailed Analysis (cont’d)
O(N1N2+(N2)2) O(N1N2)
3. N1=O(N)
N2=O(1) O(cN+c2)=O(N) O(cN)=O(N)
Solution 1 Solution 2