Original Post
Edit: Darn HTML and it's stupid, stupid symbols
I've got a class, SortedList, defined as:
Now, this is the code for my insert function, which inserts the data in ascending order, and it works:
My question is, is there a way to take this function and make it take non-linear time (in the worst case) for every insertion? I've thought about it and thought about it and linear is the only way I can come up with.
Any ideas?
aut viam inveniam aut faciam MoonStar Projects [edited by - Ronin Magus on February 18, 2003 5:23:47 PM]
template <class DataType> class SortedList : public list <DataType> with a single member function: insert(DataType data)
template <class DataType>
void SortedList <DataType>::insert(DataType data)
{
list <DataType>::iterator it = list<DataType>::begin();
list <DataType>::iterator end = list<DataType>::end();
while((it != end) && ((*it) < data))
it++;
list <DataType>::insert(it, data);
}
aut viam inveniam aut faciam MoonStar Projects [edited by - Ronin Magus on February 18, 2003 5:23:47 PM]