I have a list of random integers. I'm wondering which algorithm is used by the list::sort()
method. E.g. in the following code:
我有一个随机整数的列表。我想知道list: sort()方法使用的是哪种算法。例如,在下列守则:
list<int> mylist;
// ..insert a million values
mylist.sort();
EDIT: See also this more specific question.
看看这个更具体的问题。
3 个解决方案
#1
41
The standard doesn't require a particular algorithm, only that it must be stable, and that it complete the sort using approximately N lg N comparisons. That allows, for example, a merge-sort or a linked-list version of a quick sort (contrary to popular belief, quick sort isn't necessarily unstable, even though the most common implementation for arrays is).
标准不需要一个特定的算法,只是它必须是稳定的,并且它使用大约nlgn比较来完成排序。举例来说,这允许快速排序(与流行的想法相反,快速排序并不一定不稳定,即使数组最常见的实现是)。
With that proviso, the short answer is that in most current standard libraries, std::sort
is implemented as a intro-sort (introspective sort), which is basically a Quicksort that keeps track of its recursion depth, and will switch to a Heapsort (usually slower but guaranteed O(n log n) complexity) if the Quicksort is using too deep of recursion. Introsort was invented relatively recently though (late 1990's). Older standard libraries typically used a Quicksort instead.
但书,简短的回答是,在大多数当前的标准库,std::被实现为一个intro-sort(内省排序),这是一个快速排序,跟踪其递归深度,然后切换到一个堆排序(通常较慢但保证O(n log n)复杂性)如果快速排序使用递归的太深。不过,“内向”是最近才发明的(1990年代后期)。旧的标准库通常使用快速排序。
stable_sort
exists because for sorting array-like containers, most of the fastest sorting algorithms are unstable, so the standard includes both std::sort
(fast but not necessarily stable) and std::stable_sort
(stable but often somewhat slower).
stable_sort存在,因为对于类数组的容器进行排序,大多数最快的排序算法都是不稳定的,所以标准包括std::sort(快速但不一定稳定)和std::stable_sort(稳定但通常比较慢)。
Both of those, however, normally expect random-access iterators, and will work poorly (if at all) with something like a linked list. To get decent performance for linked lists, the standard includes list::sort
. For a linked list, however, there's not really any such trade-off -- it's pretty easy to implement a merge-sort that's both stable and (about) as fast as anything else. As such, they just required one sort
member function that's required to be stable.
然而,这两种方法通常都需要随机访问迭代器,并且在使用链表之类的东西时(如果有的话)效果会很差。要获得良好的链接列表性能,标准包括列表::sort。然而,对于一个链表,实际上并没有这样的取舍——实现一个既稳定又快速的排序是相当容易的。因此,它们只需要一个需要稳定的排序成员函数。
#2
12
It's completely implementation defined. The only thing the standard says about it is that it's complexity is O(n lg n), and that the sort is stable. That is, relative order of equal elements is guaranteed to not change after sorting.
它是完全实现定义。标准只说它的复杂性是O(nlgn)排序是稳定的。也就是说,在排序之后保证了相等元素的相对顺序不变。
std::list
's sort member function is usually implemented using some form of merge sort, because merge sort is stable, and merges are really really cheap when you are working with linked lists.
list的sort成员函数通常是使用某种形式的merge sort实现的,因为merge sort是稳定的,当你使用链表时,merge真的很便宜。
Hope that helps :)
希望帮助:)
#3
-2
Though is it implementation/vendor dependent, but most implementation I know uses Introsort whose best & worst case complexity is O(nlogn).
虽然它是依赖于实现/供应商的,但是我知道的大多数实现都使用内联排序,它的最佳和最差情况复杂度是O(nlogn)。
Reference:http://en.wikipedia.org/wiki/Introsort
参考:http://en.wikipedia.org/wiki/Introsort
#1
41
The standard doesn't require a particular algorithm, only that it must be stable, and that it complete the sort using approximately N lg N comparisons. That allows, for example, a merge-sort or a linked-list version of a quick sort (contrary to popular belief, quick sort isn't necessarily unstable, even though the most common implementation for arrays is).
标准不需要一个特定的算法,只是它必须是稳定的,并且它使用大约nlgn比较来完成排序。举例来说,这允许快速排序(与流行的想法相反,快速排序并不一定不稳定,即使数组最常见的实现是)。
With that proviso, the short answer is that in most current standard libraries, std::sort
is implemented as a intro-sort (introspective sort), which is basically a Quicksort that keeps track of its recursion depth, and will switch to a Heapsort (usually slower but guaranteed O(n log n) complexity) if the Quicksort is using too deep of recursion. Introsort was invented relatively recently though (late 1990's). Older standard libraries typically used a Quicksort instead.
但书,简短的回答是,在大多数当前的标准库,std::被实现为一个intro-sort(内省排序),这是一个快速排序,跟踪其递归深度,然后切换到一个堆排序(通常较慢但保证O(n log n)复杂性)如果快速排序使用递归的太深。不过,“内向”是最近才发明的(1990年代后期)。旧的标准库通常使用快速排序。
stable_sort
exists because for sorting array-like containers, most of the fastest sorting algorithms are unstable, so the standard includes both std::sort
(fast but not necessarily stable) and std::stable_sort
(stable but often somewhat slower).
stable_sort存在,因为对于类数组的容器进行排序,大多数最快的排序算法都是不稳定的,所以标准包括std::sort(快速但不一定稳定)和std::stable_sort(稳定但通常比较慢)。
Both of those, however, normally expect random-access iterators, and will work poorly (if at all) with something like a linked list. To get decent performance for linked lists, the standard includes list::sort
. For a linked list, however, there's not really any such trade-off -- it's pretty easy to implement a merge-sort that's both stable and (about) as fast as anything else. As such, they just required one sort
member function that's required to be stable.
然而,这两种方法通常都需要随机访问迭代器,并且在使用链表之类的东西时(如果有的话)效果会很差。要获得良好的链接列表性能,标准包括列表::sort。然而,对于一个链表,实际上并没有这样的取舍——实现一个既稳定又快速的排序是相当容易的。因此,它们只需要一个需要稳定的排序成员函数。
#2
12
It's completely implementation defined. The only thing the standard says about it is that it's complexity is O(n lg n), and that the sort is stable. That is, relative order of equal elements is guaranteed to not change after sorting.
它是完全实现定义。标准只说它的复杂性是O(nlgn)排序是稳定的。也就是说,在排序之后保证了相等元素的相对顺序不变。
std::list
's sort member function is usually implemented using some form of merge sort, because merge sort is stable, and merges are really really cheap when you are working with linked lists.
list的sort成员函数通常是使用某种形式的merge sort实现的,因为merge sort是稳定的,当你使用链表时,merge真的很便宜。
Hope that helps :)
希望帮助:)
#3
-2
Though is it implementation/vendor dependent, but most implementation I know uses Introsort whose best & worst case complexity is O(nlogn).
虽然它是依赖于实现/供应商的,但是我知道的大多数实现都使用内联排序,它的最佳和最差情况复杂度是O(nlogn)。
Reference:http://en.wikipedia.org/wiki/Introsort
参考:http://en.wikipedia.org/wiki/Introsort