排序算法的简单总结

来源:互联网 发布:爱q生活网帝国cms源码 编辑:程序博客网 时间:2024/05/18 12:01
1. STL sort:
template<class RandomIt>
void sort( RandomIt first, RandomIt last);

template<class RandomIt, class Predicate>
void sort( RandomIt first, RandomIt last, Predicate comp);

说明:
Implementation: sort() uses an introsort (quicksort which switches
to heapsort when the recursion reaches a certain depth).

Time complexity: O(nlogn).

2. STL stable_sort:
template< class RandomIt >
void stable_sort( RandomIt first, RandomIt last );

template< class RandomIt, class Compare >
void stable_sort( RandomIt first, RandomIt last, Compare comp );

说明:
Implementation: It is implemented by using merge sort.
The stable_sort algorithm is stable and guarantees that the relative
ordering of equivalent elements will be preserved.

Time complexity:
The run-time complexity of stable_sort depends on the amount of
memory available:
the best case (given sufficient memory) is O(NlogN)
and the worst case is O(N(logN)2), where N =_Last–First.
Usually, the sort algorithm is significantly faster than stable_sort.

3. STL partial_sort:
template <class RandomIt>
void partial_sort (RandomIt first, RandomIt middle, RandomIt last);

template <class RandomIt, class Compare>
void partial_sort (RandomIt first, RandomIt middle, RandomIt last, Compare comp);

说明:
From http://www.sgi.com/tech/stl/partial_sort.html:
Partial_sort rearranges the elements in the range [first, last)
so that they are partially in ascending order. Specifically,
it places the smallest middle - first elements, sorted in ascending order,
into the range [first, middle). The remaining last - middle elements are placed,
in an unspecified order, into the range [middle, last).

Implementation:
make_heap(first, middle); // maximum heap
for(RandomIt i = middle; i != last; ++i)
if(*i < *first)
__pop_heap(first, middle, i);
sort_heap(first, middle);

Time complexity: O(nlogk), n=distance(first, last), k=distance(first, middle).

4. STL nth_element:
template <class RandomIt>
void nth_element (RandomIt first, RandomIt nth, RandomIt last);

template <class RandomIt, class Compare>
void nth_element (RandomIt first, RandomIt nth, RandomIt last, Compare comp);

说明:
nth_element is a partial sorting algorithm that rearranges elements in [first, last) such that:
<1> The element at the nth position is the element that would be in that position in a sorted sequence.
<2> All of the elements before this new nth element are less than or equal to the elements after the new nth element.

Time complexity: O(n).

Select 问题: 在一个无序的数组中 找到第 n 大的元素。
思路 1: 排序,O(NlgN)
思路 2: 利用快排的 RandomizedPartition(), 平均复杂度是 O(N)
思路 3: 同样是利用快排的Partition(), 但是选择 pivot 的时候不是采用随机,而是通过一种特殊的方法。从而使复杂度最坏情况下是 O(N)。

这个partition和STL中的partition是不一样的,这个应该是我们自己常写的那个,返回划分元素的位置.
可以参考qsort.cpp 和 quick_sort.cpp.

5. STL partion:
template< class BidirIt, class UnaryPredicate >
BidirIt partition( BidirIt first, BidirIt last, UnaryPredicate p );
(until C++11)

template< class ForwardIt, class UnaryPredicate >
ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPredicate p );
(since C++11)

说明:
Rearranges the elements from the range [first,last), in such a way
that all the elements for which pred returns true precede all those
for which it returns false.
The iterator returned points to the first element of the second group.

Implementation: 可以参考quick_sort.cpp.
Time complexity: O(n).

6. STL stable_partion:
template< class BidirIt, class UnaryPredicate >
BidirIt stable_partition( BidirIt first, BidirIt last, UnaryPredicate p );

说明:
Rearranges the elements in the range [first,last), in such a way
that all the elements for which pred returns true precede all those
for which it returns false, and, unlike function partition,
the relative order of elements within each group is preserved.

The iterator returned points to the first element of the second group.

Implementation: 使用了辅助buffer,具体参考VS2013的实现.
This is generally implemented using an internal temporary buffer.

Time complexity: O(n).


7. 从效率上看,以下几种sort算法的是一个排序,效率由高到低(耗时由小变大):
partion
stable_partition
nth_element
partial_sort
sort
stable_sort

==================================================================================
8. 排序总结:
交换排序(exchange sort): bubble_sort, quick_sort(sometimes called partition-exchange sort),
选择排序(selection sort): selection_sort, heap_sort,
插入排序(insert sort): insert_sort, shell_sort,
归并排序(merge sort): merge_sort,
----------------------------------------------------
分布排序(distribution sort): bucket_sort, radix_sort,
混合排序(hybrid sort): introsort(STL的sort用的就是它)

--------------------------------------------------------------------------
| Name | best | average | worst | memory | stable | in-place |
--------------------------------------------------------------------------
| bubble_sort | n | n2 | n2 | 1 | Yes | Yes
--------------------------------------------------------------------------
| insert_sort | n | n2 | n2 | 1 | Yes | Yes
--------------------------------------------------------------------------
| select_sort | n2 | n2 | n2 | 1 | No | Yes
--------------------------------------------------------------------------
| merge_sort | nlogn| nlogn | nlogn | n | Yes | No
--------------------------------------------------------------------------
| heap_sort | nlogn| nlogn | nlogn | 1 | No | Yes
--------------------------------------------------------------------------
| shell_sort | nlogn| nlogn | nlogn | 1 | No | Yes
--------------------------------------------------------------------------
| quick_sort |nlogn | nlogn | n2 | logn | No | Yes
--------------------------------------------------------------------------

Note1.
select_sort/shell_sort/heap_sort/quick_sort are also not stable because
there are swap between two long-distance elements.
* select_sort is not stable:
i.e: [3, 3', 2, 1] => [1, 3', 2, 3] => [1, 2, 3', 3]

Note2.
quick_sort needs O(logn) space due to its recursiveness.
merge_sort also needs recursive, but it needs extra O(n) space to help, so
its space is O(n).

Note3.
Heapsort can be thought of as an improved selection sort:
like that algorithm, it divides its input into a sorted and
an unsorted region, and it iteratively shrinks the unsorted
region by extracting the largest element and moving that to
the sorted region.
The improvement consists of the use of a heap data structure
rather than a linear-time search to find the maximum.
0 0
原创粉丝点击