- 1、本文档共73页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
硕士算法2010_第4章_排序算法001讲述
算法设计与分析 李洪伟 博士 电子科技大学计算机科学与工程学院 hongweili@ /teacher/teacher.aspx?id=298 第4章 排序算法 排序的应用 Searching on unsorted data by comparing keys, optimal solutions require ?(n) comparisons. Searching on sorted data by comparing keys, optimal solutions require ?(log n) comparisons. Sorting data for users More… 排序问题 排序就是把一个无序集合中的元素按照一定顺序排列成线性结构。 例:将电话簿或字典的条目按字母表的顺序排列起来以便更有利于查找。 内部排序(internal sort):所有的数据都存储在计算机的高速随机存取存储器中,数据的读取时间可以忽略不计。 外部排序(external sort):对低速外存设备中存放的数据进行排序。 Insertion Sort Strategy: Insertion of an element in proper order: Begin with a sequence E of n elements in arbitrary order Initially assume the sorted segment contains the first element Let x be the next element to be inserted in sorted segment, pull x “out of the way”, leaving a vacancy repeatedly compare x to the element just to the left of the vacancy, and as long as x is smaller, move that element into the vacancy, else put x in the vacancy, repeat the next element that has not yet examined. Insertion Sort: e.g. Insertion Sort: Algorithm Input E, an array of elements, and n =0, the number of elements. The range of indexes is 0, …, n-1 Output E, with elements in nondecreasing order of their keys void insertionSort(Element[] E, int n) int xindex; for (xindex = 1; xindex n; xindex++) Element current = E[xindex]; key x = current.key int xLoc = shiftVacRec(E, xindex, x); E[xLoc] = current; return; Specification for subroutine Specification int shiftVacRec(Element[] E, int vacant, Key x) Precondition Vacant is nonnegative Postconditions 1. Elements in E at indexes less than xLoc are in their original positions and have keys less than or equal to x 2. Elements in E at positions xLoc+1, …, vacant are greater than x and were shifted up by one position from their positions when shiftVacRec was invoked. Algorithm shiftVacRec int shiftVacRec(Element[] E, int vacant, Key x) int xLoc; if (vacant == 0) xLoc = vacant; else if (E[vacant-1].key = x) xLoc = vacant; else E[vacant] = E[vacant-1]; xLoc = shiftVacRec(E, vaca
文档评论(0)