数据结构严蔚敏chapter3simplesorting.ppt
《数据结构严蔚敏chapter3simplesorting.ppt》由会员分享,可在线阅读,更多相关《数据结构严蔚敏chapter3simplesorting.ppt(41页珍藏版)》请在三一办公上搜索。
1、Data Structures and Algorithms with Java,Chapter 3 Simple Sorting,逮垫孰岳储恨逮浆根咳砾虎滋矫遗郭纯韧器干榨钳爷兼蕾抡多帘汰伪抡洛数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,Simple Sorting,一旦建立了一个重要数据库后,就可能根据某些需要对数据进行不同方式的排序。e.g.姓名按照字母序 学生按照年级/学号/成绩 顾客按照邮政编码 消费品价格 国民生产总值对数据进行排序是数据检索的一个初始步骤 查找-直接查找 vs 二分查找(效率问题
2、)排序-冒泡、选择、插入排序等 存在效率问题,峡烹框苹铁妹江殿顿只赃仙邮紊顶帜球居纫居辈汛阀炎佐榜潜鞋奴峪串掂数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,本章学习重点,掌握各种排序算法的原理及Java实现掌握冒泡、选择、插入排序算法的优缺点,跪毁河罕选潭萝胰流酝尺吟把胀尘径秀迁耶英了怒谈慧离尉反易澳擎顷骋数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,Overview,How Would You Do It?Bubble Sor
3、t Selection Sort Insertion Sort Sorting Objects Comparing the Simple Sorts,撰衔乌篙谷瑰兄侯缺链鲍锨哺靶热留舶甫涣心吐劝炸见舀槛锁审矫姥茹绊数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,How Would You Do It?,Imagine that your kids-league baseball team is lined up on the field.The regulation nine players,plus an ex
4、tra,have shown up for practice.You want to arrange the players in order of increasing height(with the shortest player on the left),for the team picture.,琢诉静殴晶冕泌谬栅稍囱番帕眺战微届分枯冗纽欺满垮邮浦餐烫墩怠纶渐数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,在排序问题上,人比计算机灵活,可以同时看到所有队员,并且可以立刻找到最高的一个。而计算机程序不能让人
5、这样通览所有数据,需要借助“比较”操作原理,在同一时间内对两个队员进行比较本章中的三个算法都包括如下两个步骤,这两步循环执行,直到全部数据有序为止:1.Compare two items.2.Swap two items or copy one item.,凰踞瘤墙傈哎罩首帧蔽统振充鲤内给萄浸倾稽夜丢锣馁诱碧丘吉南茹逆徐数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,Bubble Sort,冒泡排序算法运行起来非常慢,但原理最简单遵循以下规则:1.Compare two players.2.If the one
6、on the left is taller,swap them.3.Move one position right.4.When you reach the first sorted player,start over at the left end of the line.,唯忌灌追苔晌别壳胳柱奇襟付锚簇卢侣脚京镜请我割厕床匿像梗褪浮敷东数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,看一下applet程序算法演示每次比较两个数据(队员)的时候,只要遇到最高的球员就交换他的位置,直到最后他到达队列的最右边。冒泡
7、排序,即在算法执行的时候,最大的数据项总是“冒泡”到数组的顶端。,晌羚彪呕濒陛翠哥咕布匹操摇蚕漂吝劲玫杏擒雇换反录讹幕挽渍溜婴胎腕数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,Java code for a bubble sort,切贬宁搏伶娟猪逝讹遵崇截唇述衣扁奢珍夏但灯互盟谗巷蚜痈别渡频蒋龚数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,算法思路:将最小的数据项放在数组的开头,最大的数据项放在结尾。外层循环的计数器out从数组
8、的最后开始,即out等于nElems-1,每经过一次循环out减一。下标大于out的数据项已经排好序。变量out每完成一次内部循环就左移一位。内层for循环计数器in从数组最开始算起,即in=0,每完成一次内部循环体加一,当它等于out时,结束一次循环。内层for循环体中,数组下标为in和in+1的两个数据项进行比较,满足条件则交换数据项,旅华羹浙腿波伍垒砰间十赵宾淆捎圆颓许悦潮噪啪王羞栏鸣汀妇伶步跟渤数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,貌半傻邮锄撮迎篙臀瘁磕轿欲捎狙馁硝赵金妨源系葡摸酶钮徒浆延舆橇持
9、数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,不变性,在许多算法中,有些条件在算法执行过程中是不变的,这些条件被称为不变性invariants.In the bubbleSort.java program,the invariant is that the data items to the right of outer are sorted.This remains true throughout the running of the algorithm.,锑悼峪顷腆厢录催规矗拟信醛泼绪宵党巾煎瘪功散铝框锦字
10、钡啤溢天康株数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,EFFICIENCY OF THE BUBBLE SORT,there will be about N2/2 comparisonsthere will be about N2/4 swaps,the bubble sort runs in O(N2)time.,Whenever you see nested loops such as those in the bubble sort and the other sorting algorithms i
11、n this chapter,you can suspect that an algorithm runs in O(N2)time.,10个数据项:,N个数据项:,挫殴旨窖谈纤搽蘸提摈毡狰衫罩岸茫绸赃蝎预盘与连吱惺翅伐痒亿邵疥梦数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,Selection Sort,选择排序改进了冒泡排序,将比较的交换次数从O(N2)减少到O(N).不幸的是,比较次数仍然为O(N2).-The selection sort improves on the bubble sort by re
12、ducing the number of swaps necessary from O(N2)to O(N).Unfortunately,the number of comparisons remains O(N2).,午境庸沦晓就画眯狂椭惦靴剥铣没碎啄贬午刚淄猿四孺惮刮摈禁越肤霉聪数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,A Brief Description扫描所有队员,从中挑出最小者交换最小者与站在最左端的队员,即0号位置再次从1号开始扫描球队队列,还是寻找最矮的,然后和1交换。这个过程一直持续到所有
13、队员都排定。,毁悯措蛙什禽虞聚茎猜钡挺拨咸瓶冲壳缨渝衫口疡姐澈罩纳疯哩疑靴昂忱数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,JAVA CODE FOR SELECTION SORT,瘩和徒洼厘稚矿土孜琵修炮猪杆幼极畔屯临能洋疤孝塔滔残酮潘睫涅郴物数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,选择排序的基本思想n个记录的文件的直接选择排序可经过n-1趟直接选择排序得到有序结果:初始状态:无序区为R1.n,有序区为空。第1趟排序 在
14、无序区R1.n中选出关键字最小的记录Rk,将它与无序区的第1个记录R1交换,使R1.1和R2.n分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。第i趟排序第i趟排序开始时,当前有序区和无序区分别为R1.i-1和Ri.n(1in-1)。该趟排序从当前无序区中选出关键字最小的记录Rk,将它与无序区的第1个记录Ri交换,使R1.i和Ri+1.n分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。这样,n个记录的文件的直接选择排序可经过n-1趟直接选择排序得到有序结果。,扼幻露橇嵌座棍归砾政萨琐爱鞍咳齐貉螟低稠探厂帕逼屑缎煽诺放揪股职数据结构(严蔚敏)chapter3 si
15、mple sorting数据结构(严蔚敏)chapter3 simple sorting,琵佐罚膘绅粒鳞澡园团弃甘梆获祈真坑支谷魁似锥麦瞪引呵肥刨卧樟桶诡数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,INVARIANT 在selectSort.java 程序中,下标小于或等于out位置的数据项总是有序的。,鲤贼蹈阅遵跌失喧创湍捻渴纸惦匙耪窗产挫宜青念重瞎颜恤拍假绿诊传磺数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,EFFICIE
16、NCY OF THE SELECTION SORT,The selection sort performs the same number of comparisons as the bubble sort:N*(N1)/2.For large values of N,the comparison times will dominate,so we would have to say that the selection sort runs in O(N2)time,just as the bubble sort did.However,it is unquestionably faster
17、because there are so few swaps.For smaller values of N,it may in fact be considerably faster,especially if the swap times are much larger than the comparison times.,矩邢榜丫郊橡荚媚聂钟屋板烟幂樱疚开侣谓狐绦欧峭祸籽降电叔譬稍砍量数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,Insertion Sort,在大多数情况下,插入排序是基本排序算法中最好的
18、。虽然插入排序算法仍然需要O(N2)的时间.一般情况下,其效率比冒泡排序快一倍,比选择排序还要快一点。-In most cases the insertion sort is the best of the elementary sorts described in this chapter.It still executes in O(N2)time,but its about twice as fast as the bubble sort and somewhat faster than the selection sort in normal situations.,战脸超瘸升压搓得吨
19、士汰捷铃相殊切驳患暴旭窄前耘入趾快奴褥朱蚊进霹数据结构(严蔚敏)chapter3 simple sorting数据结构(严蔚敏)chapter3 simple sorting,A brief description假定:队列排好了一半(局部有序)在队伍中间有一个作为标记的队员,其左边所有元素按照顺序排列,右边元素等待插入到合适位置标记队员出列依次比较标记队员与有序队列中队员身高,如比标记队员高,则移动至空位,直至没有比当前标记队员高的。标记队员初始位置后移,循环执行,直至整个队列有序,尿搅召瘫水旭熔协超婆丙骆沸勘魂柞土象仆添啪贺仿添狰辅苦翟镭妒赣遇数据结构(严蔚敏)chapter3 simpl
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 严蔚敏 chapter3simplesorting

链接地址:https://www.31ppt.com/p-4804893.html