自己写的二分查找,如何跳出递归函数放回index

GodisaAVman 2013-05-27 03:35:13
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));
}
}
...全文
131 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
45度凝视 2013-05-27
  • 打赏
  • 举报
回复

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楼的童鞋,如果查找的数为第一个或是最后一个数时,会提示“找不到”的。
oh_Maxy 2013-05-27
  • 打赏
  • 举报
回复
这样中不:
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));
    }
}
GodisaAVman 2013-05-27
  • 打赏
  • 举报
回复
这里的return回直接返回到上一级的递归函数里面,它会执行上一阶的递归函数,并不是真正的跳出函数,我想要的是如果找到了想要的Value直接跳出递归,最好是把那个middle值作为返回值 private static int findNumberIndex(int lowIndex,int highIndex,int middle,int midValue,int[] array,int value){ return middle; }
Asura211 2013-05-27
  • 打赏
  • 举报
回复
private static void findNumberIndex(int lowIndex,int highIndex,int middle,int midValue,int[] array,int 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); } } 刚才少写了一点,改一下。。。。。
Asura211 2013-05-27
  • 打赏
  • 举报
回复
private static void findNumberIndex(int lowIndex,int highIndex,int middle,int midValue,int[] array,int value){ 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); } }
GodisaAVman 2013-05-27
  • 打赏
  • 举报
回复
自己补充一下,我想要的是如何在 if(midValue==value){ System.out.println(middle); } 就直接跳出递归函数,得到要查找的值的index(下标)

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧