算法设计(分治法-快速排序)_48287.ppt

  1. 1、本文档共41页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
算法设计(分治法-快速排序)_48287

2007-2008-01《Design and Analysis of Algorithms》SCUEC Review of last class Divide and Conquer technique The merge sort algorithm and its best-case, worst-case and average-case analysis 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 analysis of quick sort algorithm Understand the difference between merge sort and quick sort 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 A[p..r] into two sub arrays A[p..q] and A[q+1..r] Invariant: All elements in A[p..q] are less than all elements in A[q+1..r] Conquer: Sort the two sub arrays recursively Merge: Unlike merge sort, no combining step, two sub arrays form an already-sorted array Quicksort Algorithm ALGORITHM QuickSort(A, l, r) //Sorts a subarray by quicksort //Input: A subarray A[l…r] of A[0…n-1], defined by its // left and right indices l and r //Output: Subarray A[l…r] 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 Partition Clearly, all 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 In Words Given an array A[0,…,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 A[i] greater than and equal to the pivot. (3) Scan from right: Decr

您可能关注的文档

文档评论(0)

ustt002 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档