Java针对数组的普通查找法和二分查找法
2012-03-28 00:00
267 查看
下面是针对数组的普通查找法和二分查找法的示例代码
package com.jadyer.sort;
/**
* 数组查找方式
* @detail 这里演示了普通查找法和二分查找法
*/
public class ArraySearch {
public static void main(String[] args) {
int commonResult = commonSearch(new int[]{1,5,6,7,4,3,9,11,13,14,16,19,21}, 9);
int binaryResult = binarySearch(new int[]{1,3,4,6,7,8,9,12,15,17,18,20,22}, 8);
System.out.println("二分查找法: " + binaryResult);
System.out.println("普通查找法: " + commonResult);
}
/**
* 普通查找法
* @detail 该方式最好理解,同时效率也最低
*/
public static int commonSearch(int[] array, int value){
for(int i=0; i<array.length; i++){
if(value == array[i]){
return i; //返回该元素在数组中的下标
}
}
return -1; //不存在该元素则返回-1
}
/**
* 二分查找法
* @detail 要求数组有序,升序或降序均可
*/
public static int binarySearch(int[] array, int value){
int low = 0; //最小元素值的下标
int high = array.length - 1; //最大元素值的下标
int middle; //中间元素的下标
while(low <= high){
middle = (low+high) / 2;
for(int i=0; i<array.length; i++){
System.out.print(array[i]);
if(i == middle){
System.out.print("#"); //在元素后面用#号标识其为中间元素
}
System.out.print(" "); //各元素间用空格隔开
}
System.out.println();
if(value == array[middle]){
return middle;
}
if(value < array[middle]){
high = middle - 1; //右侧的不要了
}
if(value > array[middle]){
low = middle + 1; //左侧的不要了
}
}
return -1; //不存在该元素则返回-1
}
}
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/hopezhangbo/article/details/7383848
package com.jadyer.sort;
/**
* 数组查找方式
* @detail 这里演示了普通查找法和二分查找法
*/
public class ArraySearch {
public static void main(String[] args) {
int commonResult = commonSearch(new int[]{1,5,6,7,4,3,9,11,13,14,16,19,21}, 9);
int binaryResult = binarySearch(new int[]{1,3,4,6,7,8,9,12,15,17,18,20,22}, 8);
System.out.println("二分查找法: " + binaryResult);
System.out.println("普通查找法: " + commonResult);
}
/**
* 普通查找法
* @detail 该方式最好理解,同时效率也最低
*/
public static int commonSearch(int[] array, int value){
for(int i=0; i<array.length; i++){
if(value == array[i]){
return i; //返回该元素在数组中的下标
}
}
return -1; //不存在该元素则返回-1
}
/**
* 二分查找法
* @detail 要求数组有序,升序或降序均可
*/
public static int binarySearch(int[] array, int value){
int low = 0; //最小元素值的下标
int high = array.length - 1; //最大元素值的下标
int middle; //中间元素的下标
while(low <= high){
middle = (low+high) / 2;
for(int i=0; i<array.length; i++){
System.out.print(array[i]);
if(i == middle){
System.out.print("#"); //在元素后面用#号标识其为中间元素
}
System.out.print(" "); //各元素间用空格隔开
}
System.out.println();
if(value == array[middle]){
return middle;
}
if(value < array[middle]){
high = middle - 1; //右侧的不要了
}
if(value > array[middle]){
low = middle + 1; //左侧的不要了
}
}
return -1; //不存在该元素则返回-1
}
}
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/hopezhangbo/article/details/7383848
相关文章推荐
- Java中的冒泡排序法and针对数组的普通查找法和二分查找法
- 【排序】冒泡排序法and针对数组的普通查找法和二分查找法
- 【排序】冒泡排序法and针对数组的普通查找法和二分查找法
- Java实现数组的添加、删除、查找(基础型)
- java查找方法和数组反转
- 解析、查找数组中重复出现的元素(Java)
- java数组排序,查找与置换
- 数据结构(java)_数组顺序查找
- JAVA中给数组动态赋值及在数组中查找数据
- java 泛型详解(普通泛型、 通配符、 泛型接口,泛型数组,泛型方法,泛型嵌套)
- java数组中二分查找Demo范例
- java中查找数组中的重复数字
- 黑马程序员 java基础 函数 数组 查找与排序总结
- 针对java项目高CPU和高内存使用jstack命令来查找错误代码所在
- java基础--常用数组的一些操作方法-查找-最值-排序
- java折半查找法 查找数组中与目标数最接近的数
- Java基础05-数组排序与查找
- java 泛型详解(普通泛型、 通配符、 泛型接口,泛型数组,泛型方法,泛型嵌套)
- Java 数组排序及查找