08 SortedLists
08 SortedLists
info[location] = item;
length++;
}
Total time: O(N)
DeleteItem - Specification
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.
Sorted List Implementation
template<class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
list1.MakeEmpty();
list2.MakeEmpty();
list.ResetList();
What is the running time
while (!list.IsLastItem()) { using big-O?
list.GetNextItem(listItem);
if(listItem > item)
O(N2)
list2.InsertItem(listItem);
else
list1.InsertItem(listItem);
}
}
Example
• Suppose we have a million elements in an sorted
list; which algorithm would be faster?