DoublyLinkedList: Add Sort()
This commit is contained in:
@@ -365,6 +365,10 @@ public:
|
|||||||
inline int32 Count() const;
|
inline int32 Count() const;
|
||||||
// O(n)!
|
// O(n)!
|
||||||
|
|
||||||
|
template<typename Less>
|
||||||
|
void Sort(const Less& less);
|
||||||
|
// O(n^2)
|
||||||
|
|
||||||
inline Iterator GetIterator() { return Iterator(this); }
|
inline Iterator GetIterator() { return Iterator(this); }
|
||||||
inline ConstIterator GetIterator() const { return ConstIterator(this); }
|
inline ConstIterator GetIterator() const { return ConstIterator(this); }
|
||||||
|
|
||||||
@@ -645,6 +649,31 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Count() const
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
||||||
|
template<typename Less>
|
||||||
|
void
|
||||||
|
DOUBLY_LINKED_LIST_CLASS_NAME::Sort(const Less& less)
|
||||||
|
{
|
||||||
|
// selection sort
|
||||||
|
Element* tail = Head();
|
||||||
|
while (tail != NULL) {
|
||||||
|
Element* leastElement = tail;
|
||||||
|
Element* element = tail;
|
||||||
|
while ((element = GetNext(element)) != NULL) {
|
||||||
|
if (less(element, leastElement))
|
||||||
|
leastElement = element;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (leastElement != tail) {
|
||||||
|
Remove(leastElement);
|
||||||
|
InsertBefore(tail, leastElement);
|
||||||
|
} else
|
||||||
|
tail = GetNext(tail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// sGetLink
|
// sGetLink
|
||||||
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
||||||
GetLink DOUBLY_LINKED_LIST_CLASS_NAME::sGetLink;
|
GetLink DOUBLY_LINKED_LIST_CLASS_NAME::sGetLink;
|
||||||
|
|||||||
Reference in New Issue
Block a user