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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

数据结构经典算法20篇.docx

1、数据结构经典算法20篇1:最大字段问题/* * */package com.gaohongming.acm;/* * author gaohongming * */public class NMSum public static void Sum(int a ,int m ) int n = a.length; / n为数组中的个数 int b = new intn+1m+1; int SUM = new intn+1m+1; for(int p=0;p=n;p+) / 一个子段获数字都不取时 / bp0 = 0; SUMp0 = 0; / for(int p=0;p 0 时 并无意义, 此部

2、分不会被用到,注释掉/ b0p = 0;/ SUM0p = 0;/ for(int j=1;j=m;j+) for (int i = j;ib11 则舍去第一个数字 此处合理 if(SUMi-1j-1+ai-1 bij) bij = SUMi-1j-1 + ai-1; /填写SUMij供以后使用 if(jSUMi-1j) / 用bij 与之前求的比较 SUMij = bij; else SUMij = SUMi-1j; else / i = j SUMij = SUMi-1j-1 + ai-1; /end for / end for System.out.println(SUMnm); / 输

3、出结果 / end of method public static void main(String args) int a = new int1,-2,3,4,-5,-6,7,18,-9; Sum(a, 3); 2:Dijkstra算法/* * */package com.gaohongming.acm;/* * author gaohongming * */public class Dijkstra private static int N = 1000; private static int Graph = 0, 1, 5, N, N, N, N, N, N , 1, 0, 3, 7,

4、5, N, N, N, N , 5, 3, 0, N, 1, 7, N, N, N , N, 7, N, 0, 2, N, 3, N, N , N, 5, 1, 2, 0, 3, 6, 9, N , N, N, 7, N, 3, 0, N, 5, N , N, N, N, 3, 6, N, 0, 2, 7 , N, N, N, N, 9, 5, 2, 0, 4 , N, N, N, N, N, N, 7, 4, 0 ; public static void main(String args) dijkstra(0, Graph); /* * Dijkstra最短路径。 * 即图中节点vs到其它

5、各个节点的最短路径。 * param vs 起始节点 * param Graph 图 */ public static void dijkstra(int vs, int Graph) int NUM = Graph0.length; / 前驱节点数组 int prenode = new intNUM; / 最短距离数组 int mindist = new intNUM; / 该节点是否已经找到最短路径 boolean find = new booleanNUM; int vnear = 0; for (int i = 0; i mindist.length; i+) prenodei = i

6、; mindisti = Graphvsi; findi = false; findvs = true; for (int v = 1; v Graph.length; v+) / 每次循环求得距离vs最近的节点vnear和最短距离min int min = N; for (int j = 0; j Graph.length; j+) if (!findj & mindistj min) min = mindistj; vnear = j; findvnear = true; / 根据vnear修正vs到其他所有节点的前驱节点及距离 for (int k = 0; k Graph.length

7、; k+) if (!findk & (min + Graphvneark) mindistk) prenodek = vnear; mindistk = min + Graphvneark; for (int i = 0; i v + i + , s= + mindisti); 3:深度优先遍历和广度优先遍历/* * */package com.gaohongming.acm;import java.util.ArrayDeque;/* * author gaohongming *广度优先遍历和神父优先遍历 */public class BinaryTree static class Tre

8、eNode int value; TreeNode left; TreeNode right; public TreeNode(int value) this.value=value; TreeNode root; public BinaryTree(int array) root=makeBinaryTreeByArray(array,1); /* * 采用递归的方式创建一颗二叉树 * 传入的是二叉树的数组表示法 * 构造后是二叉树的二叉链表表示法 */ public static TreeNode makeBinaryTreeByArray(int array,int index) if(

9、indexarray.length) int value=arrayindex; if(value!=0) TreeNode t=new TreeNode(value); arrayindex=0; t.left=makeBinaryTreeByArray(array,index*2); t.right=makeBinaryTreeByArray(array,index*2+1); return t; return null; /* * 深度优先遍历,相当于先根遍历 * 采用非递归实现 * 需要辅助数据结构:栈 */ public void depthOrderTraversal() if(r

10、oot=null) System.out.println(empty tree); return; ArrayDeque stack=new ArrayDeque(); stack.push(root); while(stack.isEmpty()=false) TreeNode node=stack.pop(); System.out.print(node.value+ ); if(node.right!=null) stack.push(node.right); if(node.left!=null) stack.push(node.left); System.out.print(n);

11、/* * 广度优先遍历 * 采用非递归实现 * 需要辅助数据结构:队列 */ public void levelOrderTraversal() if(root=null) System.out.println(empty tree); return; ArrayDeque queue=new ArrayDeque(); queue.add(root); while(queue.isEmpty()=false) TreeNode node=queue.remove(); System.out.print(node.value+ ); if(node.left!=null) queue.add(

12、node.left); if(node.right!=null) queue.add(node.right); System.out.print(n); /* * 13 * / * 65 5 * / * 97 25 37 * / / / * 22 4 28 32 */ public static void main(String args) int arr=0,13,65,5,97,25,0,37,22,0,4,28,0,0,32,0; BinaryTree tree=new BinaryTree(arr); tree.depthOrderTraversal(); tree.levelOrde

13、rTraversal(); 4:线性查找/* * */package com.gaohongming.acm;/* * author gaohongming * 线性查找 */public class LineSearch public static void main(String args) / TODO Auto-generated method stub / 输入数据数组 int a = 12, 76, 29, 22, 15, 62, 29, 58, 35, 67, 58, 33, 28, 89, 90, 28, 64, 48, 20, 77 ; LineSearch line = n

14、ew LineSearch(); line.lineSearch(a, 22); /* * 线性查找 * param a * param e */ private void lineSearch(int a, int e) / TODO Auto-generated method stub / 数据索引计数变量 int count = 1; for (int i = 0; i = end) return -1; else if (key srcArraymid) return binSearch(srcArray, mid + 1, end, key); else if (key srcArr

15、aymid) return binSearch(srcArray, start, mid - 1, key); return -1; / 二分查找普通循环实现 public static int binSearch(int srcArray, int key) int mid = srcArray.length / 2; if (key = srcArraymid) return mid; int start = 0; int end = srcArray.length - 1; while (start = end) mid = (end - start) / 2 + start; if (

16、key srcArraymid) start = mid + 1; else return mid; return -1; 6:堆排序/* * */package com.gaohongming.acm;/* * author gaohongming * 堆排序 */public class HeapSortTest public static void main(String args) int data5 = new int 5, 3, 6, 2, 1, 9, 4, 8, 7 ; print(data5); heapSort(data5); System.out.println(排序后的数

17、组:); print(data5); public static void swap(int data, int i, int j) if (i = j) return; datai = datai + dataj; dataj = datai - dataj; datai = datai - dataj; public static void heapSort(int data) for (int i = 0; i = 0; i-) / 保存当前正在判断的节点 int k = i; / 若当前节点的子节点存在 while (2 * k + 1 = lastIndex) / biggerInd

18、ex总是记录较大节点的值,先赋值为当前判断节点的左子节点 int biggerIndex = 2 * k + 1; if (biggerIndex lastIndex) / 若右子节点存在,否则此时biggerIndex应该等于 lastIndex if (databiggerIndex databiggerIndex + 1) / 若右子节点值比左子节点值大,则biggerIndex记录的是右子节点的值 biggerIndex+; if (datak databiggerIndex) / 若当前节点值比子节点最大值小,则交换2者得值,交换后将biggerIndex值赋值给k swap(dat

19、a, k, biggerIndex); k = biggerIndex; else break; public static void print(int data) for (int i = 0; i data.length; i+) System.out.print(datai + t); System.out.println(); 7:快速排序/* * */package com.gaohongming.acm;/* * author gaohongming * 快速排序 */public class FastSort public static void main(String arg

20、s) System.out.println(Hello World); int a = 12,20,5,16,15,1,30,45,23,9; int start = 0; int end = a.length-1; sort(a,start,end); for(int i = 0; istart) /从后往前比较 while(endstart&aend=key) /如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较 end-; if(aendstart&astart=key) int temp = astart; astart = aend; aend = temp; /此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用 /递归 if(startlow) sort(a,low,start-1

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

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