算法及其相关
来源:互联网 发布:vmware11 mac os补丁 编辑:程序博客网 时间:2024/06/05 13:23
算法基础
队列:先进先出,后进后出
public class dui { private Object[] objects; private int size; private int head; private int end; public dui(int size){ this.objects = new Object[size]; this.head = 0; this.end = 0; this.size = 0; } public void push(Object object) throws Exception{ if(this.size>objects.length) throw new Exception("Queue is full"); objects[end++] = object; size++; } public Object pop()throws Exception{ if(this.size == 0) // return null; throw new Exception("Queue is empty!"); if(head == objects.length) this.head = 0; size--; return objects[head++]; } public Object peek() throws Exception{ if(this.size == 0) throw new Exception("Queue is empty!"); return objects[head]; }}
栈:先进后出,后进先出
public class Stack { private Object[] objects; private int head; private int size; public Stack (int size){ objects = new Object[size]; this.head = 0; this.size = 0; } public void push(Object object) throws Exception{ if (this.size == objects.length) throw new Exception("this stack is full"); objects[head++] = object; size++; } public Object pop() throws Exception{ if (size == 0) throw new Exception("this stack is empty"); size--; return objects[--head]; }}
斐波那契数列两种实现:递归和非递归
//递归方法public class feibo { private static int getFibo(int i){ if (i==1) return 1; if (i==2) return 1; else return getFibo(i-1)+getFibo(i-2); } public static void main(String[] args){ for(int j=1;j<=20;j++){ System.out.print(getFibo(j)+"\t"); if(j % 5 == 0) System.out.println(); } }}
冒泡排序
public class mao { public static void main(String[] args) { int[] f={3,5,7,12,9,13,14}; int a; for(int i=0;i<f.length;i++){ for(int j=0;j<f.length-i-1;j++){ if(f[j]<f[j+1]) { a=f[j]; f[j]=f[j+1]; f[j+1]=a; } } } System.out.print("最终循环结果" + " "); for (int i : f) { System.out.print(i + " "); } }}
快速排序
public class Array { public static int partition(int []array,int lo,int hi){ int key=array[lo]; while(lo<hi){ while(array[lo]<=key&&hi>lo){//从前边找大的 lo++; }// array[hi]=array[lo]; while(array[hi]>=key&&hi>lo){//从后边找小的 hi--; } array[lo]=array[hi]; } array[hi]=key; return hi; } public static void sort(int []array,int lo,int hi){ if(lo>=hi){ return; } int index =partition(array,lo,hi); sort(array,lo,index-1); sort(array,index+1,hi); } public static void main(String[] args) { int []array={49,27,97,76,13,65,38}; sort(array,0,array.length-1); for(int i:array){ System.out.print(i+" "); } } }
阅读全文
0 0
- SIFT算法及其相关
- 拼图游戏及其相关算法
- 算法及其相关
- 处理器调度及其相关算法
- 阶乘相关的算法及其C++实现
- 扩展欧几里得算法及其相关应用
- 18_01_加密类型及其相关算法
- 磁盘的访问机制及其相关算法
- 目标跟踪算法汇总及其相关论文
- 并行算法研究现状及其相关问题的综述
- 无向图最小割的相关算法及其延伸
- 快速傅里叶变换的相关定义、原理及其递归算法
- 【算法导论学习-28】Fibonacci数列及其相关
- 二分查找及其相关算法的java实现
- linux学习之路之加密类型及其相关算法
- 容易理解的快速排序算法及其相关算法的总结(含java实现方法)
- 子窗口及其相关
- SSL 及其相关知识
- 树状数组
- C++ STL 第三次实验
- Gridview适配器及viewpager禁止滑动
- 关于java的重载(overload)和重写(override)的区别
- 【NOI2002】银河英雄传说
- 算法及其相关
- bzoj 2654 tree (二分 + 最小生成树)
- POJ-2482-Stars in Your Window(线段树,扫描线)
- java学习记录——使用Arrays.sort对数组进行升序排序
- [ Java Zero to One ] 0. 从 0 到 1 Java Tech
- Java中的this和super
- [ACM]CCF CSP [201409-5]E题 拼图
- 二.tablayout_NewsFragment
- Ubuntu16.04+Titan Xp+Tensorflow1.3+Cuda8.0+CuDNN6