二进制搜索程序编译但不起作用

时间:2013-01-28 21:04:13

标签: java

// program: BinSearch1.java
//
import java.util.*;

class BinSearch1
{
    public static void main(String[] args)
    {
        final int size = 50;
        Scanner console = new Scanner(System.in);

        // declaring arrays
        int[]  item = new int[size];    // The list of numbers
        int    items=0;     // number of values in list
        int location;
        int seekValue;

        // method returns number of items entered by user.
        // user enters –1 to end entry process.
        //
        items = createInOrder( item );

        System.out.println("Element\t\tValue");

        // Display the current list
        //
        for( int i=0; i < items; i++ )
            System.out.println( i + "\t\t" + item[i] );

        System.out.print("Enter a value to seek: ");
        seekValue = console.nextInt();

        location = binSearch( item, items, seekValue);

        if( location >= 0 )
          System.out.println("Element found at location " + location);
        else
          System.out.println("Element not found in list");
    }

    // method: createInOrder
    //   This method will fill in a previously created array with values
    // in increasing order. When complete the method will return the number
    // of values inserted into the list.
    //
    public static int createInOrder( int[] n )
    {
        int    i;
        int    value=0;
        int    items=0;
        Scanner console = new Scanner(System.in);

        while( items < n.length && value >= 0 )
        {
            System.out.print("Enter a positive number to insert (negative to quit): ");
            value = console.nextInt();
            if( value >= 0 )
            {
                // insert into array

                // step 1- locate the proper space for the item.
                i=0;
                while( i < items && n[i] < value)
                    i++;

                // step 2- shift others down
                for( int j=items-1; j >=i; j--)
                    n[j+1] = n[j];

                // step 3- insert new item
                n[i] = value;
                items++;

            }
        }

        console.close();
        // return number of values entered
        return items;   
    }

   // INSERT MISSING CODE HERE! Write method binSearch
   /*
   public static void binSearch(int[] x, int y, int j)
   {
       int v, a;
       int[] p;
       for(int i=0; i < x.length; i++)
       {
           if(v < x[i])
           {
               x[i]=a;
               return a;
           }
           if(v> x[i])
           {
               x[i]/2=p[i];
               p[i]=a;
               return a;
           }

       }
   }
   public static void binSearch(int[] x, int y, int j)
   {
       int i;
       for(i=0; i<y.length; i++)
       {
           if( y.length/2 == x[i])
           {
               if(
            }
       }
    }
    */

    public static int binSearch (int[] arrayName, int items, int seekItem ) 
{

  int bottom = 0;
  int top = arrayName.length - 1;
  int middle;
  boolean found = false;
  int location = -1;

  while (bottom <= top && !found) {

    middle = (bottom + top) / 2;

    if(arrayName[middle] == seekItem) {            
      found = true;
      location = middle;
    } else if (arrayName[middle] < seekItem) {  
      bottom = middle + 1;
    } else {
      top = middle - 1;
    }
  }
  return location;
}


}

我正在编写我的老师指定的二进制搜索程序。它编译并且我可以输入数字,但是当我尝试寻找值并获得正确的索引时,它告诉我元素每次都不在列表中。有什么问题?

1 个答案:

答案 0 :(得分:1)

错误在于“top”的初始化:

public static int binSearch (int[] arrayName, int items, int seekItem ) 
{

int bottom = 0;
int top = arrayName.length - 1;  // is always 49

试试这个:

public static int binSearch (int[] arrayName, int items, int seekItem ) 
{
int bottom = 0;
int top = items - 1;