java面试题9--数组高级-二分查找

来源:互联网 发布:大学网络宣传部 编辑:程序博客网 时间:2024/04/27 19:45

首先明确一点:
二分查找是有条件限制的,被查找的数组要求一定是有序

原理图:

这里写图片描述

这里有两个函数,分别使用非递归递归思想实现了二分查找的功能

package search;import java.util.Scanner;public class BinarySearch {    /**     * 函数功能:非递归二分查找     * 输入参数:arg1:被查找的数组     *          arg2:目标数字     * return:目标数字的index,若找不到返回-1     */    public static int binarySearchWithoutR(int[] arr,int target){        //定义查找段的开始head        int head = 0;        //定义查找段的结尾tail        int tail = arr.length-1;        System.out.println("查找的索引从"+head+"->"+tail);        int middle = 0;        while(head<=tail){            //定义需要被比较的中间数            middle = (head + tail)/2;             if(arr[middle]==target){                return middle;            }else if(target<arr[middle]){                tail = middle-1;            }else if(target>arr[middle]){                head = middle+1;            }        }        return -1;    }    /**     * 函数功能:递归二分查找     * 输入参数:arg1:被查找的数组     *          arg2:head--定位查找段的开始index     *          arg3:tail--定位查找端的结束index     *          arg4:target--目标数字     * return:目标数字的index,若找不到返回-1     */    public static int binarySearchWithR(int[] arr, int head, int tail, int target){        //定义需要被比较的中间数        int middle = (head+tail)/2;        while(head<tail){            if(arr[middle]==target){                return middle;            }else if(target<arr[middle]){                return binarySearchWithR(arr,head,tail = middle-1,target);            }else if(target>arr[middle]){                return binarySearchWithR(arr,head=middle+1,tail,target);            }        }        return -1;    }    /**     * 格式打印数组     * @param arr     */    public static void printArray(int [] arr){        System.out.print("[");        for(int x=0;x<arr.length;x++){            if(x==arr.length-1){                System.out.print(arr[x]+"]");                System.out.println();            }else{                System.out.print(arr[x]+", ");            }        }    }    /**     * @param args     */    public static void main(String[] args) {        //定义查找对象数组        int [] a ={1, 4, 5, 8, 15, 33, 36, 41, 48, 77, 81, 90, 94, 96};        printArray(a);        System.out.println("请输入要查找的数,回车后开始查找");        Scanner scan = new Scanner(System.in);        int target = new Integer(scan.next());            System.out.println("正在启动查找程序.....");        //调用非递归查找函数        System.out.println("调用非递归查找函数----------------");        int res = binarySearchWithoutR(a, target);        //判断结果        if(res==-1){            System.out.println("没有找到!!数组中不存在这个数");        }else{            System.out.println("找到了!index是:"+res);        }        //调用递归查找函数        System.out.println("调用递归查找函数-----------------");        int res1 = binarySearchWithR(a,0,a.length-1,target);        //判断结果        if(res1==-1){            System.out.println("没有找到!!数组中不存在这个数");        }else{            System.out.println("找到了!index是:"+res1);        }    }}

查找结果演示:

[1, 4, 5, 8, 15, 33, 36, 41, 48, 77, 81, 90, 94, 96]请输入要查找的数,回车后开始查找36正在启动查找程序.....调用非递归查找函数----------------查找的索引从0->13找到了!index是:6调用递归查找函数-----------------找到了!index是:6
0 0
原创粉丝点击