package BinarySearch;
public class BinarySearchClass
{ public static int binary_search(int[] array, int n, int value)
{
n = array.length;
int lowIndex = 0; //begin index of the array
int highIndex = n - 1;//end index of the array
int middle = (lowIndex + highIndex)/2;//middle index of the array
int midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
return -1;
}
private static void findNumberIndex(int lowIndex,int highIndex,int middle,int midValue,int[] array,int value){
if(midValue==value){
System.out.println(middle);
}else if(midValue<value){
lowIndex = middle ;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}else{
highIndex = middle;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}
} public static void main(String[] args)
{
int array[] = {1,2,3,5,6,7,8,9,11,22,33,44,55,66,77,88,99,111,222,333,345,444,555,666,777,888};
System.out.println(binary_search(array, 11, 555));
}
}二分查找递归搜索
public class BinarySearchClass
{ public static int binary_search(int[] array, int n, int value)
{
n = array.length;
int lowIndex = 0; //begin index of the array
int highIndex = n - 1;//end index of the array
int middle = (lowIndex + highIndex)/2;//middle index of the array
int midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
return -1;
}
private static void findNumberIndex(int lowIndex,int highIndex,int middle,int midValue,int[] array,int value){
if(midValue==value){
System.out.println(middle);
}else if(midValue<value){
lowIndex = middle ;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}else{
highIndex = middle;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}
} public static void main(String[] args)
{
int array[] = {1,2,3,5,6,7,8,9,11,22,33,44,55,66,77,88,99,111,222,333,345,444,555,666,777,888};
System.out.println(binary_search(array, 11, 555));
}
}二分查找递归搜索
if(midValue==value){
System.out.println(middle);
}
就直接跳出递归函数,得到要查找的值的index(下标)
if(middle==lowIndex||middle==highIndex) {
System.out.println("未能找到该数");
if(midValue==value){
System.out.println(middle);
return;
}else if(midValue<value){
lowIndex = middle ;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}else{
highIndex = middle;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}
}
if(middle==lowIndex||middle==highIndex) {
System.out.println("未能找到该数");
return;
}
if(midValue==value){
System.out.println(middle);
return;
}else if(midValue<value){
lowIndex = middle ;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}else{
highIndex = middle;
middle = (lowIndex + highIndex)/2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}
}刚才少写了一点,改一下。。
private static int findNumberIndex(int lowIndex,int highIndex,int middle,int midValue,int[] array,int value){
return middle;
}
public class BinarySearchClass
{ public static int binary_search(int[] array, int n, int value)
{
n = array.length;
int lowIndex = 0; //begin index of the array
int highIndex = n - 1;//end index of the array
int middle = (lowIndex + highIndex) / 2;//middle index of the array
int midValue = array[middle];
return findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
} private static int findNumberIndex(int lowIndex, int highIndex, int middle, int midValue,
int[] array, int value)
{
if (middle == lowIndex || middle == highIndex)
{
System.out.println("未能找到该数");
return -1;
} if (midValue == value)
{
System.out.println(middle);
return middle;
}
else if (midValue < value)
{
lowIndex = middle;
middle = (lowIndex + highIndex) / 2;
midValue = array[middle];
return findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}
else
{
highIndex = middle;
middle = (lowIndex + highIndex) / 2;
midValue = array[middle];
return findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}
} public static void main(String[] args)
{
int array[] =
{1, 2, 3, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 345, 444,
555, 666, 777, 888};
System.out.println(binary_search(array, 11, 555));
}
}
package BinarySearch;
public class BinarySearchClass
{ public static int binary_search(int[] array, int value) {
int n = array.length; // n作为参数传递后又赋值没有意义,直接去掉。
if(n == 0){ // 空数组,无意义
System.out.println("空数组");
return -1;
}
int lowIndex = 0; // 低位
int highIndex = n; // 这里需要把高位变为数组的长度,因为middle永远不等于highIndex
int middle = (lowIndex + highIndex) / 2;// 中位
int midValue = array[middle]; // 中值
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
return -1;
} private static void findNumberIndex(int lowIndex, int highIndex,
int middle, int midValue, int[] array, int value) {
//System.out.println("low = " + lowIndex + "; middle = " + middle + "; high = " + highIndex);
if (midValue == value) { // 要先判断值,再进行递归操作;将跳出判断提前会遗漏扫描
System.out.println(middle); // 这里的middle就是要查找的值的下标
return;
}
if(middle == lowIndex){ // middle永远不可能等于highIndex,如果middle==lowIndex就代表已经扫面完毕
System.out.println("未找到");
return;
}
if (midValue < value) { // 向后查找
lowIndex = middle;
middle = (lowIndex + highIndex) / 2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
} else { // 向前查找
highIndex = middle;
middle = (lowIndex + highIndex) / 2;
midValue = array[middle];
findNumberIndex(lowIndex, highIndex, middle, midValue, array, value);
}
} public static void main(String[] args) {
int array[] = { 1 , 2, 3 , 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88,
99, 111, 222, 333, 345, 444, 555, 666, 777, 888 };
binary_search(array, 3);
}}
5楼的童鞋,如果查找的数为第一个或是最后一个数时,会提示“找不到”的。