《C++ STL-数据结构与算法实现》课件第9章 排序相关算法_第1页
《C++ STL-数据结构与算法实现》课件第9章 排序相关算法_第2页
《C++ STL-数据结构与算法实现》课件第9章 排序相关算法_第3页
《C++ STL-数据结构与算法实现》课件第9章 排序相关算法_第4页
《C++ STL-数据结构与算法实现》课件第9章 排序相关算法_第5页
已阅读5页,还剩26页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第九章排序相关算法排序_Sort第n小元素nth_element二分搜索binary_search归并排序Merge有序集操作set_堆与堆排序_heap1/32Arrangestheelementsinaspecifiedrangeinto

anondescending

order1

oraccordingtoanorderingcriterionspecifiedbyabinarypredicate2.template<classRandomAccessIterator,classPredicate>void

sort(

RandomAccessIteratorfirst, //排序区间[first,last)

RandomAccessIteratorlast,

Predicatecomp); //二元谓词2:排序准则 //缺省1:operator<支持RandomAccess的容器:vector,deque,string,arraylist不能用该算法链表不支持随机访问排序Sort2/32Arrangestheelementsinaspecifiedrangeintoanondescendingorderoraccordingtoanorderingcriterionspecifiedbyabinarypredicateand

preservestherelativeorderingofequivalentelements.稳定排序:排序前后,等值元素的相对位置关系不变!template<classBidirectionalIterator,classBinaryPredicate>void

stable_sort(BidirectionalIterator_First,BidirectionalIterator_Last,BinaryPredicate_Comp);排序stable_sort3/32Arranges

aspecifiednumberofthesmallerelementsinarangeintoanondescending1orderoraccordingtoanorderingcriterionspecifiedbyabinarypredicate2.template<classRandomAccessIterator,classBinaryPredicate>voidpartial_sort(

RandomAccessIteratorfirst, //源区间[first,last)RandomAccessIteratorsortEnd, //部分排序:最小元素个数RandomAccessIteratorlast

BinaryPredicatecomp

);//可略。二元谓词2:排序准则 //缺省1:operator<支持RandomAccess的容器:vector,deque,string,arraypartial_sort_copy(…)

自学排序partial_sort4/32Returnstrueiftheelementsinthespecifiedrangeareinsortedorder.template<classForwardIterator,classBinaryPredicate>bool

is_sorted(

ForwardIterator_First,ForwardIterator_Last,BinaryPredicate_Comp);ReturnsaForwardIteratorthatissettothelastelementthatisin

sortedorderfromaspecifiedrange.template<classForwardIterator,classBinaryPredicate>ForwardIterator

is_sorted_until(

ForwardIterator_First,ForwardIterator_Last,

BinaryPredicate_Comp

);//可略。二元谓词:排序准则 //缺省:operator<排序is_sorted5/32例:sort等结构体变量装入容器*it要适应各种

类型的元素6/32例:sort等容器元素的类型?7/32Partitionsarangeofelements,correctlylocatingthenthelementofthesequenceintherangesothatalltheelementsinfrontofitarelessthanorequaltoitandalltheelementsthatfollowitinthesequencearegreaterthanorequaltoit.比较:QuickSort的分区template<classRandomAccessIterator,classBinaryPredicate>void

nth_element(

RandomAccessIterator_First, //操作范围:[_First,_Last)RandomAccessIterator_Nth, //S=thenthelementRandomAccessIterator_Last,

BinaryPredicate_Comp);//二元谓词:比较条件,缺省:<第n小元素nth_element第n最小值排在第n位8/32时间复杂度比较(例)问题:班上有30个学生,想知道某课程分数排在倒数第4名的学生算法:???方案:sort:排升序,取第4位。缺点:?partial_sort:对分数最小的前4位排升序,取第4位。缺点:?

前4位有必要排序吗?nth_element:第4最小值排在第4位,取第4位。效率更好。第n小元素nth_element9/32例:nth_element分别对v1,v2,v3作nth_element看看不同n值的结果有何差异?用自编谓词:比较条件用系统预定义谓词:比较条件自编二元谓词10/32Testswhetherthereisanelementinasortedrangethatisequaltoaspecifiedvalue

orthatisequivalenttoitinasensespecifiedbyabinarypredicate.template<classForwardIterator,classType,classBinaryPredicate>bool

binary_search(

ForwardIteratorfirst, //搜索范围ForwardIteratorlast,constType&value, //指定值

BinaryPredicatecomp

); //谓词:比较条件。缺省:<ReturnValue:true,满足条件要求:排序序列二分搜索binary_search11/32Findsthepositionofthefirstelementinanorderedrangethathasavaluegreaterthanorequivalenttoaspecifiedvalue,wheretheorderingcriterionmaybespecifiedbyabinarypredicate.template<classForwardIterator,classType,classBinaryPredicate>ForwardIterator

lower_bound(ForwardIteratorfirst,ForwardIteratorlast,constType&value, //指定值

BinaryPredicatecomp

);//缺省:<Return:

第一个

>=value

的元素位置ForwardIterator

upper_bound(

……//同上

BinaryPredicatecomp); //缺省:<Return:

第一个>value

的元素位置(没有=)二分搜索lower_/upper_boundlower_boundupper_boundvalue=512/32Givenanorderedrange,findsthesubrangeinwhichallelementsareequivalenttoagivenvalue.template<classForwardIterator,classType,classPredicate>pair<ForwardIterator,ForwardIterator>

equal_range(ForwardIteratorfirst,ForwardIteratorlast,constType&val, //指定值

Predicatecomp); //一元谓词。 //缺省:<Return:Thefirstiteratorofthepairislower_bound,andtheseconditeratorisupper_bound.[>=value,…,>value)二分搜索equal_rangelower_boundupper_boundvalue=513/32例:二分搜索相关算法怎么对list排序?等于5的元素第一个2位置最后一个3位置4的范围回顾4的个数14/32Combinesalloftheelementsfromtwosortedsourceranges

into

asingle,sorteddestinationrange,wheretheorderingcriterionmaybespecifiedbyabinarypredicate.功能:合并两个有序区间(同序)到一个有序区间。template<classInputIterator1,classInputIterator2,classOutputIterator,classBinaryPredicate>OutputIterator

merge(

InputIterator1_First1, //源区域1:已排序InputIterator1_Last1,InputIterator2_First2, //源区域2:已排序InputIterator2_Last2,

OutputIterator_Result //合并结果:已排序

BinaryPredicate_Comp

); //比较准则:按此排序 //缺省<:非降序Remarks:Theoperationisstable.稳定算法MSextension:unchecked_merge,checked_merge归并排序Merge15/32分治法:MergeSort思想Merge算法16/32Combinestheelementsfromtwoconsecutive连续的sortedranges

into

asinglesortedrange,wheretheorderingcriterionmaybespecifiedbyabinarypredicate.功能:合并一个区间内(未序)、相邻的两个有序子区间(同序)。template<classBidirectionalIterator,classPredicate>voidinplace_merge(BidirectionalIteratorfirst,//操作范围[first,last)BidirectionalIteratormiddle,//[first,middle),[middle,last)BidirectionalIteratorlast,

Predicate_Comp

); //缺省<归并排序inplace_merge17/32例:归并排序合并a,b到c,升序要求a,b?合并a,b到c要求:降序用d数组给v赋值v内进行合并排序18/32includes算法Testswhetheronesortedrangecontains

alltheelementscontainedinasecondsortedrange,wheretheorderingorequivalencecriterionbetweenelementsmaybespecifiedbyabinarypredicate.功能:子集判断S2∈S1,允许重复元素。template<classInputIterator1,classInputIterator2,classBinaryPredicate>bool

includes(

InputIterator1_First1, //S1:有序集合InputIterator1_Last1,InputIterator2_First2, //S2:有序集合InputIterator2_Last2,

BinaryPredicate_Comp

); //排序规则。缺省:<Returnture:

range1包含range2有序集操作includes19/32set_union算法Unitesalloftheelementsthatbelongtoatleastoneoftwosortedsourcerangesintoasingle,sorteddestinationrange,wheretheorderingcriterionmaybespecifiedbyabinarypredicate.功能:求并集S1∪S2,允许重复元素。重复元素如何处理?若某元素在S1出现n次,S2出现m次,并集出现:max(n,m)次template<classInputIterator1,classInputIterator2,classOutputIterator,classBinaryPredicate>OutputIterator

set_union(InputIterator1_First1, //S1:有序集合InputIterator1_Last1,InputIterator2_First2, //S2:有序集合InputIterator2_Last2,

OutputIterator_Result, //sorteddestinationrange

BinaryPredicate_Comp

);

//orderingcriterion.缺省:<有序集操作set_union20/32set_intersectionUnitesalloftheelementsthatbelongtobothsortedsourcerangesintoasingle,sorteddestinationrange,wheretheorderingcriterionmaybespecifiedbyabinarypredicate.功能:求交集S1∩S2,允许重复元素。

若某元素在S1出现n次,S2出现m次,交集出现:min(n,m)次template<classInputIterator1,classInputIterator2,classOutputIterator,classBinaryPredicate>OutputIterator

set_intersection(InputIterator1_First1, //S1:有序集合InputIterator1_Last1,InputIterator2_First2, //S2:有序集合InputIterator2_Last2,

OutputIterator_Result, //sorteddestinationrange

BinaryPredicate_Comp

);

//orderingcriterion.缺省:<有序集操作set_intersection21/32set_differenceUnitesalloftheelementsthatbelongtoonesortedsourcerange,butnottoasecondsortedsourcerange,intoasingle,sorteddestinationrange,wheretheorderingcriterionmaybespecifiedbyabinarypredicate.功能:求差集S1-S2,S1中不属于s2的元素。允许重复元素。若某元素在S1出现n次,S2出现m次,差集出现:max(n-m,0)次template<classInputIterator1,classInputIterator2,classOutputIterator,classBinaryPredicate>OutputIterator

set_difference(InputIterator1_First1, //S1:有序集合InputIterator1_Last1,InputIterator2_First2, //S2:有序集合InputIterator2_Last2,

OutputIterator_Result, //sorteddestinationrange

BinaryPredicate_Comp

);

//orderingcriterion.缺省:<有序集操作set_difference22/32set_symmetric_differenceUnitesalloftheelementsthatbelongtoone,butnotboth,ofthesortedsourcerangesintoasingle,sorteddestinationrange,wheretheorderingcriterionmaybespecifiedbyabinarypredicate.功能:求对称差集(S1-S2)∪(S2-S1),允许重复元素。若某元素在S1出现n次,S2出现m次,对称差出现:|n-m|次template<classInputIterator1,classInputIterator2,classOutputIterator,classBinaryPredicate>OutputIterator

set_difference(InputIterator1_First1, //S1:有序集合InputIterator1_Last1,InputIterator2_First2, //S2:有序集合InputIterator2_Last2,

OutputIterator_Result, //sorteddestinationrange

BinaryPredicate_Comp

);

//orderingcriterion.缺省:<有序集操作set_symmetric_difference23/32例:有序集相关操作ASCII:大写在前,小写在后输出数组每个元素自编谓词:排序规则以数组为例24/32例:有序集相关操作25/32回顾

在容器之priority_queue已讲堆概念,这里介绍相关函数。make_heapConverts

elementsfromaspecifiedrangeintoaheapinwhichthefirstelementisthelargestandforwhichasortingcriterionmaybespecifiedwithabinarypredicate.最大堆:逻辑结构是具有父母优势的完全二叉树,存储结构是一维数组template<classRandomAccessIterator,classBinaryPredicate>void

make_heap(

RandomAccessIterator_First, //specifiedrangeRandomAccessIterator_Last,

BinaryPredicate_Comp

); //sortingcriterion //缺省<:最大值堆(大根堆)支持RandomAccess的容器:vector,deque,string,array堆与堆排序make_heap26/32pop_heapRemovesthelargestelementfromthefrontofaheaptothenext-to-lastpositionintherangeandthenformsanewheapfromtheremainingelements.next-to-lastposition:last-1template<classRandomAccessIterator,classBinaryPredicate>void

pop_heap(RandomAccessIterator_First, //rangeRandomAccessIterator_Last,

BinaryPredicate_Comp

); //比较规则 //缺省<:大根堆说明:只是remove而非earse,删除的元素放在最后位置:l

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论