奇怪的效率好吧,所以下面:std :: list.sort()
我想使用双链表,并希望对其中的对象进行排序。这些对象属于另一个类。奇怪的是:当我使用我自己创建的双链表时,我在将它们添加到列表的时刻排序元素。这就像插入排序,所以效率n^2。
但是,这比使用std :: list排序更快,其中我将所有元素添加到列表并在此之后调用sort()。我真的不明白为什么......
欲了解更多信息:在我排序~500个元素的时刻。
我在做错了什么名单?
编辑:一些代码
代码为的std ::列表
for every object:
list.push_back(object);
list.sort();
我的列表:
struct node{
someObject* data;
float depth;
node* prev;
node* next;
node(someObject* o){
data = o;
prev = NULL;
next = NULL;
depth = depthFunc(o);
}
float depthFunc(someObject* o);
};
struct myList{
node* head;
node* tail;
void insertAfter(node* toAdd, node* n);
void insertBefore(node* toAdd, node* n);
void addNode(someObject* o){
node* n = new node(o);
if (head == NULL) {
head = n;
tail = n;
} else {
node* temp = head;
while(temp != NULL && temp->depth < n->depth){
temp = temp->next;
}
if (temp != NULL) insertBefore(n, temp);
else insertAfter(n, tail);
}
}
};
我想你的n^2是最差的情况下的复杂度,如果你幸运的话,取决于数据(预分类)它可以更好 – piokuc 2013-04-22 17:33:38
你在测量这个? – Macmade 2013-04-22 17:56:41
请提供一个[SSCCE](http://sscce.org/) – TemplateRex 2013-04-22 18:04:53