电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

算法课件全121301ADA07分治法快速排序

41页
  • 卖家[上传人]:E****
  • 文档编号:91095428
  • 上传时间:2019-06-21
  • 文档格式:PPT
  • 文档大小:410.50KB
  • / 41 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、2019/6/21,2,2012-2013-01 Design and Analysis of Algorithm SCUN,Review of last class,Divide and Conquer technique Master theorem The merge sort algorithm and its best-case, worst-case and average-case analysis,Divide And Conquer (II),Chapter 4,Application to sorting problem Quick sort,2019/6/21,4,2012-2013-01 Design and Analysis of Algorithm SCUN,Goals of the Lecture,At the end of this lecture, you should Master the idea of quick sort algorithm Master the best-case, worst-case and average-case an

      2、alysis of quick sort algorithm Master the difference between merge sort and quick sort,2019/6/21,5,2012-2013-01 Design and Analysis of Algorithm SCUN,Quicksort,Another sort algorithm example to reveal the essence of divide-and-conquer technique Its idea can be described as follows: Divide: Partition the array Apr into two sub arrays Apq and Aq+1r Invariant: All elements in Apq are less than all elements in Aq+1r Conquer: Sort the two sub arrays recursively Merge: Unlike merge sort, no combining

      3、step, two sub arrays form an already-sorted array,2019/6/21,6,2012-2013-01 Design and Analysis of Algorithm SCUN,Quicksort Algorithm,ALGORITHM QuickSort(A, l, r) /Sorts a subarray by quicksort /Input: A subarray Alr of A0n-1, defined by its / left and right indices l and r /Output: Subarray Alr sorted in nondecreasing order if l r s Partition(A, l, r) /s is a split position QuickSort(A, l, s-1) QuickSort(A, s+1, r) end if,2019/6/21,7,2012-2013-01 Design and Analysis of Algorithm SCUN,Clearly, al

      4、l the action takes place in the divide step should be the followings: Select a pivot and rearranges the array in place End result: Two sub arrays been separated by the pivot The elements in one sub array are smaller than the pivot The elements in another are larger than the pivot Returns the index of the “pivot” element separating the two sub arrays How do you suppose we implement this?,Partition,2019/6/21,8,2012-2013-01 Design and Analysis of Algorithm SCUN,Partition In Words,Given an array A0,

      5、n-1, we can partition the array like these: (1) Initial: select an element to act as the “pivot” (which?), let i and j indicate the index of second left and right elements which will be used to compare with the pivot. (2) Scan from left: Increase i until Ai greater than and equal to the pivot. (3) Scan from right: Decrease j until Aj smaller than and equal to the pivot. (4) Swap Ai and Aj (5) Repeat (2),(3) and (4) until ji. (6) Swap Ai and Aj, swap A0 and Aj, return j.,2019/6/21,9,2012-2013-01

      6、Design and Analysis of Algorithm SCUN,Partition example,Initial list,6, 7, 5, 2, 5, 8,6, 5, 5, 2, 7, 8,6, 5, 5, 2, 7, 8,Done!,2, 5, 5 6 7, 8,2019/6/21,10,2012-2013-01 Design and Analysis of Algorithm SCUN,Partition Algorithm(I),ALGORITHM Partition1(A, l, r) /Input: A subarray Alr of A0n-1, defined by its left / and right indices l and r (lr) /Output: A partition of Alr, with the split position / returned as this functions value p Al i l; j r + 1 repeat repeat i i + 1 until Ai p repeat j j - 1 un

      7、til Aj p swap(Ai, Aj); until i j swap(Ai, Aj) / undo last swap when i j swap(Al, Aj) return j / j is the final index of pivot,Any Problem ?,Why not “ ?,2019/6/21,11,2012-2013-01 Design and Analysis of Algorithm SCUN,Partition Algorithm(II),ALGORITHM Partition2(A, l, r) /Input: A subarray Alr of A0n-1, defined by its left / and right indices l and r (lr) /Output: A partition of Alr, with the split position / returned as this functions value p Al; j l for i l +1 to r do if Ai p j j + 1 if j i swap

      8、(Aj, Ai) end if end for swap(Al, Aj) return j / j is the final index of pivot,2019/6/21,12,2012-2013-01 Design and Analysis of Algorithm SCUN,Quicksort Example,2019/6/21,13,2012-2013-01 Design and Analysis of Algorithm SCUN,Analyzing Quicksort,What will be the best case for the algorithm? Partition is perfectly balanced, this means that the array is divided into two equal length subarrays. In the best case: Tbest(1) = 0 Tbest(n) = 2Tbest(n/2) + n-1 What does the recurrence relation work out to?

      9、T(n) = (nlogn),2019/6/21,14,2012-2013-01 Design and Analysis of Algorithm SCUN,Analyzing Quicksort,What will be the worst case for the algorithm? Partition is always unbalanced Will any particular input elicit the worst case? Yes: Already-sorted input In the worst case, Partition will do n-1 comparisons, but create one partition that has n-1 elements and the other will have no elements Because we wind up just reducing the partition by one element each time, worst case is given by: T(1) = 0 T(n) = T(n - 1) + n - 1 The recurrence relation works out to T(n) = (n2),2019/6/21,15,2012-2013-01 Design and Analysis of Algorithm SCUN,Improving Quicksort,The real liability of quicksort is that it runs in O(n2

      《算法课件全121301ADA07分治法快速排序》由会员E****分享,可在线阅读,更多相关《算法课件全121301ADA07分治法快速排序》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    监控施工 信息化课堂中的合作学习结业作业七年级语文 发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2
     
    收藏店铺
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.