Algorithm.Find(查找)
来源:互联网 发布:应聘java简历自我评价 编辑:程序博客网 时间:2024/06/14 09:57
二分搜索
查找第K小
package Algorithm.Find;import java.util.Collections;import java.util.Vector;/*Author: CPlusPlus小码农 *If any question, *Please contact: * http://daixiecplusplus.blog.163.com/ * QQ:1926742804 */public class BinarySearch<E extends Comparable<E> > {public int BSearch(Vector<E> v, E value){int low = 0;int high = v.size()-1;int mid;E temp;while(low <= high){mid = (low+high)/2;temp = v.get(mid);if( temp.compareTo(value) == 0) return mid;else if(temp.compareTo(value) < 0){low = mid+1;}else{high = mid-1;}}return -1;}public int BSearchByExcursion(Vector<E> v,E value,int low,int high){if(low > high) return -1;else{int mid = (low+high)/2;E temp = v.get(mid);if(temp.compareTo(value) == 0) return mid;else if(temp.compareTo(value) < 0){return BSearchByExcursion(v,value,mid+1,high);}else return BSearchByExcursion(v,value,low,mid-1);}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubBinarySearch<Integer> bs = new BinarySearch<Integer>();Vector<Integer> v = new Vector<Integer>();v.add(10);v.add(20);v.add(50);v.add(100);v.add(15);v.add(60);Collections.sort(v);System.out.println(bs.BSearch(v, 5));System.out.println(bs.BSearch(v, 50));System.out.println(Collections.binarySearch(v, 50));System.out.println(bs.BSearchByExcursion(v, 5, 0, v.size()-1));System.out.println(bs.BSearchByExcursion(v, 50, 0, v.size()-1));}}
package Algorithm.Find;import java.util.Iterator;import java.util.Vector;import Structure.BSTree.BSTree;/*Author: CPlusPlus小码农 *If any question, *Please contact: * http://daixiecplusplus.blog.163.com/ * QQ:1926742804 */public class FindKthMin<E extends Comparable<E> > {public E Find_KthMin(Vector<E> v, int kth){BSTree<E> tree = new BSTree<E>();Iterator<E> i = v.iterator();while(i.hasNext()){tree.Insert(i.next());}return tree.FindKth(kth);}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubVector<Integer> v = new Vector<Integer>();v.add(10);v.add(50);v.add(20);v.add(30);v.add(40);FindKthMin<Integer> fkth = new FindKthMin<Integer>();System.out.println(fkth.Find_KthMin(v, 5));}}
0 0
- Algorithm.Find(查找)
- STL algorithm算法find(16)
- 查找(搜索)算法(**Search algorithm**)
- STL algorithm find
- STL algorithm find() demo
- Algorithm: Find the maximum
- c++:algorithm-find
- 5.查找命令(find)
- algorithm-查找、排序
- [Algorithm]二分法查找算法
- 查找(find)
- find 查找
- Algorithm Part I:Union-Find
- 查找并复制(find,xargs,cp)
- 【Linux】 find指令(文件查找)
- 查找(whereis locate which find)
- LintCode Find Peak Element(查找峰值)
- linux常用命令-查找文件(find/locate)
- android-support-multidex解决65535问题
- android的进程间通信----aidl
- OK6410开发板bootloader架构设计分析---嵌入式回归第五篇
- 三步翻转法------有意思的方法
- Lab3:自行车码表
- Algorithm.Find(查找)
- 类的权限修饰符
- 用例图——详解
- WebService学习总结(一)——WebService的相关概念
- 彻底解决 Android Studio 乱码问题
- LeetCode *** 284. Peeking Iterator
- 计算机网络中的一些概念
- wps-奇数偶数页眉不同设定方法
- JSP基础整理