简体   繁体   English

c ++矢量排序

[英]c++ vector sorting

This is a generic question about the mechanism behind sorting using the STL std::sort function. 这是关于使用STL std :: sort函数进行排序的机制的一般性问题。 I've read some posts about sorting and that, in general, sorting vectors is faster than sorting linked lists. 我读过一些关于排序的帖子,一般来说,排序向量比排序链表更快。 Is this true for vectors and linked lists of structures/objects? 这对于矢量和结构/对象的链表是否正确? For a linked list of structures, I feel sorting can easily be achieved by just modifying the indexes. 对于结构的链接列表,我觉得只需修改索引就可以轻松实现排序。 On the other hand, sorting for vectors seems like it would involve physically switching the data locations of the data of the structure/object since they are contiguous (is this true?). 另一方面,向量的排序似乎涉及物理地切换结构/对象的数据的数据位置,因为它们是连续的(这是真的吗?)。 In that case it seems sorting linked lists would be faster. 在这种情况下,似乎排序链表会更快。

EDIT!!!: Now with picture: 编辑!!!:现在有了图片:

在此输入图像描述

So I guess the question is better phrased: Which is faster for sorting objects, sorting a linked list OR a vector (although this might depend on the size of the object)? 所以我想问题是更好的措辞:哪个更快排序对象,排序链表或矢量(虽然这可能取决于对象的大小)? Also, is the sorting for a linked list done as shown in 3) and is the sorting done for a vector done as showing in 2)? 此外,是否按照3)所示完成了链接列表的排序,并且对于完成如2)所示的向量进行了排序?

Sorting of list is specialized (ie list has function sort and cannot be sorted with std::sort ). list排序是专门的(即list具有函数排序,不能使用std::sort )。

void sort();
template <class Compare> void sort(Compare comp);

Complexity: Approximately N log(N) comparisons, where N == size(). 复杂性:大约N log(N)比较,其中N == size()。

std::sort in generalized. std::sort in generalized。

template<class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);

Complexity: O(N log(N)) (where N == last - first) comparisons. 复杂性:O(N log(N))(其中N == last - first)比较。

Note that result of std::list::sort is same as std::stable_sort . 请注意, std::list::sort的结果与std::stable_sort相同。 Note that there are no information in standard, how sort should be realized. 请注意,标准中没有信息,应该如何实现sort It's implementation-defined. 它是实现定义的。

You can always sort by sorting a parallel collection of indices or pointers or whatever. 您总是可以通过排序索引或指针的并行集合或其他任何方式进行排序。 This works for both lists and vectors. 这适用于列表和向量。

The reason sorting a list is slower is the fastest sorting algorithms require random access, ie the ability to be able to fetch immediately the value at a given index. 排序列表较慢的原因是最快的排序算法需要随机访问,即能够立即获取给定索引处的值的能力。 You don't get that with lists. 你没有得到列表。 To get the 10th item in a linked list (say) you have to start at the beginning and move forward 10 times. 要获得链接列表中的第10个项目(比如说),您必须从头开始并向前移动10次。

You're correct, a sort of std::vector is going to be slow if copying an element is slow. 你是对的,如果复制一个元素很慢,一种std::vector会很慢。 C++11 introduces move semantics which might help, elements can be moved instead of copied. C ++ 11引入了可能有帮助的移动语义,可以移动元素而不是复制元素。

A linked list is quite easy to sort with a merge sort, which is O(n log n) the same as any other good sorting algorithm. 链接列表很容易使用合并排序进行排序,其中O(n log n)与任何其他良好排序算法相同。 The difference is in the overhead. 不同之处在于开销。

As always benchmarking your particular case is a good idea if the results are important to you. 如果结果对您很重要,那么始终对您的特定情况进行基准测试是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM