二分排序
来源:互联网 发布:mac 安装包已损坏 编辑:程序博客网 时间:2024/06/02 19:43
public class BinarySearch { public static void main(String[] args) { int[] arr = new int[]{7,10, 11, 12, 16, 18, 23, 29, 33, 48, 54, 57, 68, 77, 84, 98}; int index = search(arr, 29); System.out.println(index); } public static int search(int[] arr, int key) { int index = -1; int startIndex = 0; int endIndex = arr.length-1; int middleIndex = -1; while(startIndex <= endIndex) { middleIndex = (startIndex + endIndex)/2; if(key < arr[middleIndex]) { endIndex = middleIndex - 1; }else if(key > arr[middleIndex]) { startIndex = middleIndex + 1; }else { index = middleIndex; break; } } return index; }}
阅读全文
0 0
- 二分排序
- 二分排序
- 二分排序
- 二分排序
- 二分排序
- 排序+二分
- 二分排序
- 二分排序
- 二分排序
- 排序之二分排序
- 二分排序与二分查找
- 二分排序与二分查找
- 二分排序与二分查找
- 二分查找和二分排序
- 二分排序:直接插入排序+二分搜索
- 二分查找+二分排序(快速排序)
- 排序之二分插入排序
- 排序之二分插入排序
- 10.JDBC
- angularjs checkbox
- 11.流、序列化
- Java —— 对文件进行多个Object对象流的读写操作
- 12.类、抽象类、匿名类、内部类、继承、接口、泛型
- 二分排序
- 个人对专注力的一点看法
- 13.工具类、XML、动态代理
- JAVA-包装类
- Java多线程问题汇总
- 14.设计模式、单例模式
- Linux相关命令
- 循环
- 15.开放问题