排序算法的简单总结
来源:互联网 发布:爱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 switchesto 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 relativeordering of equivalent elements will be preserved.Time complexity:The run-time complexity of stable_sort depends on the amount ofmemory 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 heapfor(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 waythat all the elements for which pred returns true precede all thosefor 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 waythat all the elements for which pred returns true precede all thosefor 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算法的是一个排序,效率由高到低(耗时由小变大):partionstable_partitionnth_elementpartial_sortsortstable_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 becausethere 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, soits 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 andan unsorted region, and it iteratively shrinks the unsortedregion by extracting the largest element and moving that tothe sorted region.The improvement consists of the use of a heap data structurerather than a linear-time search to find the maximum.
0 0
- 简单排序算法的总结
- 排序算法的简单总结
- 简单的排序算法总结
- 简单排序算法总结
- 简单排序算法总结
- 简单排序算法总结
- 【复习总结】一些简单的排序算法
- 几种排序算法的简单总结
- 简单选择排序算法总结
- 算法--简单排序算法总结与模板
- 简单的排序算法
- 简单的排序算法
- 简单的排序算法
- 数据结构及简单算法的总结----之【排序】
- 面试必备-最简单的排序算法清晰的排序算法总结
- 排序算法的总结
- 排序算法的总结
- 排序算法的总结
- 黑马程序员--java基础--IO流(二)
- c#添加picturebox自定义可选图片按键
- python下通过串口发送结构体数据
- 协同过滤推荐算法
- uva11038 How Many O's? 数位dp
- 排序算法的简单总结
- 《C++语言基础》实践项目——运算符重载(二)
- 移动应用统计的基本原理及 UMID 方案解析
- (转)给刚毕业的大学生和刚入门的程序员的忠告及图书推荐
- JDK的动态代理为什么必须要使用接口与使用CGLIB动态代理
- c++引用参数与引用返回值
- SQL数据库通用数据导出工具(一)
- Android atomic
- 工作日志2015-04-27