二进制搜索,返回键值。

时间:2016-02-04 12:51:07

标签: java sorting search binary

我正在尝试创建一个二进制搜索程序,它返回我的"位置"的值。 (我们搜索的值所在的数组的索引)但是,作为赋值的一部分,我不允许直接从我的binarySearch方法打印。有没有人知道我可以从我的main方法打印搜索结果的方法。谢谢!

import java.util.Scanner;
import java.util.Arrays;
// in - Scanner
// value - String input from user
// v - Integer input from user 
// counter - Integer for counting down elements remaining
// i - counting slave
// key - object we are searching for
public class HelloWorld
{
//********************************************************************************************************************
    public static void main(String[]args)
    {
        Scanner in = new Scanner(System.in);//create reference to new scanner
        System.out.println("Enter the integer size of the array you wish to create.");
        String value = in.nextLine();//value = size of the array
        int v = Integer.parseInt(value);//parse to int
        String[]sampleArray;//creates ref to array
System.out.println("Debug: int v = "+v);
        sampleArray=new String[v];//size of the array
        int counter = v;
            for(int i = 0; i < sampleArray.length; i++,v--)//until the array is full..
                {
                    if(i == sampleArray.length-1)
                        {
                            System.out.println("Enter the last element of the array.");
                            sampleArray[i] = in.nextLine();
                        }
                    else
                        {
                            System.out.println("Enter an element to the array. You have to add "+v+" more elements.");
                            sampleArray[i] = in.nextLine();
                        }
                }
        System.out.println("Enter Key.");
        String key2 = in.nextLine();
//********************************************************************************************************************
    for (int i = 1; i < sampleArray.length; i++)
        {
            String key = sampleArray[i];
            int position = i;
                while (position > 0 && key.compareTo(sampleArray[position-1]) < 0)
                    {
                        sampleArray[position] = sampleArray[position-1];
                        position--;
                    }
                sampleArray[position] = key;
        }
//********************************************************************************************************************
int i = sampleArray.length-1;
    while(i >= 0) 
        {
            System.out.print(sampleArray[i]+" ");
            i = i - 1;
        }
        System.out.println("done sorting..");
    binarySearch(sampleArray,0,sampleArray.length,key2);
    System.out.println(binarySearch(position));//**HERE IS MY ERROR**
    }
//********************************************************************************************************************
public static int binarySearch(String[] array, int lowerbound, int upperbound, String key)
   {
    int position;

    position = (lowerbound + upperbound) / 2;

    while((array[position].compareTo(key) == 0) && (lowerbound <= upperbound))
    {
         if (array[position].compareTo(key) > 0)            // If the number is > key, ..
        {                                                   // decrease position by one. 
              upperbound = position - 1;
        }
              else
        {
              lowerbound = position + 1;                    // Else, increase position by one. 
        }
       position = (lowerbound + upperbound) / 2;
    }
        if (lowerbound <= upperbound)
        {
           return position;
        }
        else
        {
            return-1;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你的代码错了。

  • 您尚未声明position变量。
  • 您无法调用binarySearch(position),因为该函数需要4个参数。

只需通过猜测binarySearch(position)您希望打印找到它的元素的位置,我建议采用以下解决方案:

更改代码的这一部分:

binarySearch(sampleArray,0,sampleArray.length,key2);
System.out.println(binarySearch(position));

要:

int position = binarySearch(sampleArray,0,sampleArray.length,key2);
System.out.println(position);

这很好用,我认为这就是你想要的。