关于二元搜索问题的家庭作业

时间:2016-11-11 15:28:31

标签: java

我正在尝试将十进制数组放入一个数组并使用二进制搜索来搜索它,但它并不是我想要的。这是我的代码。我需要使用这个来获得数字45.3,我不知道我做错了什么,所以请帮助。

class Project3
{
    public static void main(String[] args)
    {
        double[] data= new double[6];
        data[0]=-3;
        data[1]=10;
        data[2]=5;
        data[3]=24;
        data[4]=45.3;
        data[5]=10.5;
        binarySearch(data[4]);
    }
    public boolean binarySearch(int key)
    {
        int size=6;
        int low=0;
        int high=size-1;
        while(high>=low)
        {
            int middle = (low + high) / 2;
            if(data[middle] == key)
            {
                return true;
            }
            if(data[middle] < key)
            {
                low=middle+1;
            }
            if(data[middle]>key)
            {
                high=middle-1;
            }
        }
        return false;
    }
}

2 个答案:

答案 0 :(得分:0)

首先你应该阅读二进制搜索是什么。它仅适用于已排序的数组。以排序顺序插入数据或创建方法对数据进行排序。

`

 class Project3
{
    public static void main(String[] args)
    {
        double[] data= new double[6];
        data[0]=-3;
        data[1]=10;
        data[2]=5;
        data[3]=24;
        data[4]=45.3;
        data[5]=10.5;
        binarySearch(data[4]);//are you doing any thing with the result.
    }
    public boolean binarySearch(int key)// you can not pass a double value to int.
    {
        int size=6;//It's better to use data.length instead of hard coding it.
        int low=0;
        int high=size-1;
        while(high>=low)
        {
            int middle = (low + high) / 2;
            if(data[middle] == key)//from where are you passing the data array.
            {
                return true;
            }
            if(data[middle] < key)
            {
                low=middle+1;
            }
            if(data[middle]>key)
            {
                high=middle-1;
            }
        }
        return false;
    }
}

`

以下代码将有效。 `

public class Project3 {
    static double[] data;
    public static void main(String[] args) {
        data = new double[6];
        data[0] = -3;
        data[1] = 5;
        data[2] = 10;
        data[3] = 10.5;
        data[4] = 24;
        data[5] = 44.5;
        System.out.println(binarySearch(data[4]));
        System.out.println(binarySearch(90));
    }
    public static boolean binarySearch(double key) {
        int size = data.length;
        int low = 0;
        int high = size - 1;
        while (high >= low) {
            int middle = (low + high) / 2;
            if (data[middle] == key)
                return true;
            if (data[middle] < key)
                low = middle + 1;
            if (data[middle] > key)
                high = middle - 1;
        }
        return false;
    }
}

`

答案 1 :(得分:-2)

将密钥更改为double。然后使数组成为一个成员变量,你可以在整个类中访问它。

static double[] data = new double[6];

public static void main(String[] args) {
    data[0] = -3;
    data[1] = 10;
    data[2] = 5;
    data[3] = 24;
    data[4] = 45.3;
    data[5] = 10.5;
    System.out.println(binarySearch(data[4]));
}

public static boolean binarySearch(double key) {
    int size = 6;
    int low = 0;
    int high = size - 1;
    while (high >= low) {
        int middle = (low + high) / 2;
        if (data[middle] == key) {
            return true;
        }
        if (data[middle] < key) {
            low = middle + 1;
        }
        if (data[middle] > key) {
            high = middle - 1;
        }
    }
    return false;
}

这段代码对我有用并得到正确的答案