ImageVerifierCode 换一换
格式:DOCX , 页数:25 ,大小:28.71KB ,
资源ID:11543259      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11543259.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构与算法.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构与算法.docx

1、数据结构与算法数据结构与算法1 算法1.1 查找查看文档: 第7章.pdf为了分析实施键比较的算法和性能,我们应该使用键比较次数作为算法所做工作量的度量标准。1.1.1 顺序查找平均比较次数为 n(n+1)/2n;1.1.2 二分查找一、前置条件操作对象为有序表。二、算法描述between bottom and topmiddle = (bottom + top)/2;if target middle bottom = middle + 1; repeat the above processelse top = middle; repeat the above processend if 1.

2、1.3 查找树2-树二叉查找树查看文档: 13351072_郑泽丰_11_1The expected complexity of a search, insert, or delete operation is O(logn), while the worst-case complexity isO(height) = O(n), where nis number of nodes/elements.递归实现用递归的思想实现插入、搜索和删除等操作。Remove From A Degree 2 Node。Replace with largest key in left subtree (or s

3、mallest in right subtree).数组实现查看文档: 13351072_郑泽丰_11_2链表实现红黑树Colored Nodes DefinitionBinary search tree.Each node is colored redor black.Root and all external nodes are black.No root-to-external-node path has two consecutive red nodes.All root-to-external-node paths have the same number of black node

4、s 词典查找树1.1.4 插值查找1.1.5 外部查找B-树1.2 排序查看文档: 第8章.pdf1.2.1 插入排序查看文档: 2413553.html算法描述假定n是数组的长度,首先假设第一个元素被放置在正确的位置上,这样仅需从1-n-1范围内对剩余元素进行排序。对于每次遍历,从0-i-1范围内的元素已经被排好序,每次遍历的任务是:通过扫描前面已排序的子列表,将位置i处的元素定位到从0到i的子列表之内的正确的位置上。将arri复制为一个名为target的临时元素。向下扫描列表,比较这个目标值target与arri-1、arri-2的大小,依次类推。这个比较过程在小于或等于目标值的第一个元素

5、(arrj)处停止,或者在列表开始处停止(j=0)。在arri小于前面任何已排序元素时,后一个条件(j=0)为真,因此,这个元素会占用新排序子列表的第一个位置。在扫描期间,大于目标值target的每个元素都会向右滑动一个位置(arrj=arrj-1)。一旦确定了正确位置j,目标值target(即原始的arri)就会被复制到这个位置。与选择排序不同的是,插入排序将数据向右滑动,并且不会执行交换。效率分析稳定 空间复杂度O(1) 时间复杂度O(n2) 最差情况:反序,需要移动n*(n-1)/2个元素 最好情况:正序,不需要移动元素数组在已排序或者是“近似排序”时,插入排序效率的最好情况运行时间为O

6、(n);插入排序最坏情况运行时间和平均情况运行时间都为O(n2)。通常,插入排序呈现出二次排序算法中的最佳性能。对于具有较少元素(如n=15)的列表来说,二次算法十分有效。在列表已被排序时,插入排序是线性算法O(n)。在列表“近似排序”时,插入排序仍然是线性算法。在列表的许多元素已位于正确的位置上时,就会出现“近似排序”的条件。通过使用O(nlog2n)效率的算法(如快速排序)对数组进行部分排序,然后再进行选择排序,某些高级的排序算法就是这样实现的。数组版templatevoid Sortable_list:insertion_sort() int first_unsorted;int pos

7、ition;Record current;for(first_unsorted=1;frst_unsortedcount;first_unsorted+)if(entryfirst_unsorted0&entryposition-1current);entryposition = current; 链式版只需要把未排序的鱼啊元素与原链断开,然后插入到应有的位置即可 1.2.2 选择排序一、介绍In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort.

8、It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory

9、is limited.二、算法描述The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty an

10、d the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to th

11、e right. 三、分析Selection sort does have the advantage of predictability: Its worst-case time will differ littlefrom its best.The function swap is called n-1 times, and eachcall does 3 assignments of entries, for a total count of 3(n-1). There are (n-1)+(n-2)+1 = (1/2)n(n-1) comparisons of keys, which

12、we approximate to(1/2)n2+O(n).平均来说,插入排序所进行的比较哦啊次数大约仅是选择排序的一半,如果表内的元素较小,移动起来不慢的话,则插入排序会比选择排序快。数组版templatevoid Sortable_list:selection_sort() for(int position = count-1;position0;position-) int max = max_key(0,position);swap(max,position);The function max_key() is to find the maximum value in the part

13、 that the parameters gave.链式版1.2.3 希尔排序Shell sort.结合插入排序和选择排序的优点,采用缩小增量的方法。先把所有元素每隔n个进行分组并排序,然后再每个m个进行分组排序,直到每隔1个为止。最后进行的是普通的插入排序。templatevoid Sortable_list:shell_sort() int increment,start; increment=count; do increment = increment/3 + 1; for(start = 0;start1);函数sort_interval(start,increment)表示起始地址

14、为start,增量为increment的插入排序。1.2.4 分而治之排序void Sortable_list:sort() if th list has length greater than1 partition the list into lowlist,highlist;lowlist.sort();highlist.sort();combine(lowlist,highlist);快速排序每次选择一个支点(pivot),将整个序列按照大于或者等于支点和小于支点的规则分为两个部分。依次递归至所分成的部分的元素数目为1为止。以下是顺序实现的快速排序伪代码if low_position h

15、igh_positionseperate the list into two partion with the pivot between low_position and high_position.recursively do the above operation.end if.归并排序Conceptually, a merge sort works as follows:Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted

16、).Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.归并排序的主要操作有:1.将序列分成两部分;2.将两个部分进行排序整合;3.递归以上操作直至分成两个部分之后只剩一个元素。顺序实现的归并排序算法在空间上会消耗较多。 1.2.5 堆排序首先,对序列进行堆初始化,因为堆排序仅适用于顺序表。然后,向堆中添加元素时,先添加到堆的最末尾。然后一路回溯至堆根部。回溯过程中,判断是否满足堆的条件。堆排序

17、的时间复杂度为O(nlogn).1.2.6 树排序1.2.7 拓扑排序查看文档: 77145191.前置条件G是一个无有向环的有向图。则G的拓扑次序是G中所有顶点的一个顺序排列2.偏序和全序3.常用的算法有Kahn算法和DFS算法两者区别Kahn算法不需要检测图为DAG,如果图为DAG,那么在出度为0的集合为空之后,图中还存在没有被移除的边,这就说明了图中存在环路。而基于DFS的算法需要首先确定图为DAG,当然也能够做出适当调整,让环路的检测和拓扑排序同时进行,毕竟环路检测也能够在DFS的基础上进行。二者的复杂度均为O(V+E)。广度优先算法一、无前趋的顶点优先的拓扑排序方法该方法的每一步总是

18、输出当前无前趋(即入度为零)的顶点,其抽象算法可描述为:NonPreFirstTopSort(G)/优先输出无前趋的顶点while(G中有入度为0的顶点)do从G中选择一个入度为0的顶点v且输出之;从G中删去v及其所有出边;if(输出的顶点数目|V(G)|)/若此条件不成立,则表示所有顶点均已输出,排序成功。Error(G中存在有向环,排序失败!); 深度优先算法一、pseudocodethat will contain the sorted nodesS Set of all nodes with no outgoing edgesfor each node n in S do visit(

19、n) function visit(node n) if n has not been visited yet then mark n as visited for each node m with an edgefrom m to ndo visit(m) add n to L二、code in C+(见文档链接)三、继的顶点优先拓扑排序方法1、思想方法该方法的每一步均是输出当前无后继(即出度为0)的顶点。对于一个有向图,按此方法输出的序列是逆拓扑次序。因此设置一个栈(或向量)T来保存输出的顶点序列,即可得到拓扑序列。若T是栈,则每当输出顶点时,只需做入栈操作,排序完成时将栈中顶点依次出栈即

20、可得拓扑序列。若T是向量,则将输出的顶点从Tn-1开始依次从后往前存放,即可保证T中存储的顶点是拓扑序列。2、抽象算法描述算法的抽象描述为:NonSuccFirstTopSort(G)/优先输出无后继的顶点while(G中有出度为0的顶点)do 从G中选一出度为0的顶点v且输出v;从G中删去v及v的所有人边if(输出的顶点数目|V(G)|)Error(G中存在有向环,排序失败!);Kahn算法一、eudocodeL Empty list that will contain the sorted elementsS Set of all nodes with no incoming edgesw

21、hile S is non-empty do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into Sif graph has edges then return error (graph has at least one cycle)else return L (a topologically sorted or

22、der)二、注意1.需要记录的有:顶点的连接关系、入度为0的顶点集合、存放排好序的顶点2.需要的方法:判断顶点入度是否为0、移走一条边、统计排好序的顶点 1.3 表格信息检索1.3.1 基数排序1.3.2 哈希排序查看文档: 13351072_郑泽丰_10_1worst-case time for search,insert,and delete is O(size);Expected time is O(1).Ideal HashingUses a 1D array (or table) table0:b-1.Each position of this array is a bucket.A

23、 bucket can normally hold only one record.Uses a hash function f thatconverts each keyk into an index in the range0, b-1.f(k)is the home bucketfor key k.Every record (key, element)is stored in its home bucket tablefkey.A collisionoccurs when the home bucket for a new record is occupied by a record w

24、ith a different key.An overflow occurs when there is no space in the home bucket for the new pair.When a bucket can hold only one record, collisions and overflows occur together.Need methods to handle collisions (and overflows).Hash Table IssuesSize (number of buckets) of hash table.Choice of hash f

25、unction.collisions handling method.Two principal criteria:It should be easy andquick to compute.Convert key into an integer in case the key is not an integer.Done by the method hashCode().It shouldachieve an even distribution of the keys that actually occur across the range of indices.f(k)is an inte

26、ger in the range 0, b-1, where bis the number of buckets in the table.choosing a hash function1.TruncationThe first,second, and fifth digits from the right might make the hash function, so that 21296876 maps to 976.2.Folding21296876 maps to 212+968+76 = 1256.3.Modular ArithmeticThe best choice for m

27、odulus is often, but not always, a prime number. Collision Resolution with Open AddressClusteringLinear ProbingIncrement FunctionsQuadratic ProbingKey-Dependent IncrementsRandom Probing 1.4 图的遍历1.4.1 广度优先搜索1.Discription:Visit start vertex and put into a FIFO queue.Repeatedly remove a vertex from the

28、 queue,visit its unvisited adjacent vertices,put newly visited vertices into the queue.2.PropertyAll vertices reanchable from the start vertex(including the start vertex) are visited.1.4.2 深度优先搜索1.pseudocode:depthFirstSearch(v) Lable vertex v as reached.for(each unreached vertex u adjacenct from v)d

29、epthFirstSearch(u);2.prpgertiessame complexity as BFS; 1.5 贪心算法1.5.1 最短路径查看文档: uid-27164517-id-3287891.htmlDijkstra算法查看文档: uid-20384806-id-1954190.html一、前置条件1.单源最短路径2.有向图或者无向图3.所有边权非负二、算法描述Let the node at which we are starting be called the initial node. Let the distance of node Y be the distance from the initial node to Y. Dijkstras algorithm will assign some initial distance values and will try to improve them step by step.1.Assign to every node a tentative distance value: set it to zero for our initial node and to infi

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1