从文本练习二进制搜索

时间:2014-04-05 21:06:48

标签: java arrays search binary

编写一个程序,从文本文件中读取整数,然后对数字进行排序。然后使用笔记中的二进制搜索算法(第6周)要求用户输入一个数字,如果该数字不在列表中,则显示该数字的位置。

到目前为止我的代码:

import java.util.Arrays;

public class myBinarySearch
{
    public static void main(String[] args)
    {
        int i;
        int target, found;
        int[] numArray = {36, 27, 29, 15, 16, 39, 11, 31};

        Arrays.sort(numArray);
        for(i=0; i <numArray.length; i++)
        System.out.print(numArray[i] + " ");

        System.out.print("\nEnter the number you are searching for: ");
        target = ReadKb.getInt();
        found = theBinarySearch(numArray, target);
        if(found == -1)
        System.out.println("Number not found");
        else
        System.out.println("Number: " +target +" found at position: " +found);
    }

    //Method searching using binary search algorithm
    private static int theBinarySearch(int []numArray, int target)
    {
        int mid,bottom,top;
        mid=0;
        bottom=0;
        top=numArray.length-1;
        while (top>= bottom)
        {
            mid=(top +bottom)/2;
            if(target==numArray[mid])
                break;
            else
                if(target<numArray[mid])
                    top=mid-1;
                else
                    bottom=mid+1;
        }

        if(target==numArray[mid])
            return mid;
        else
            return -1;
    }

}

和txt文档:

numbers.txt  55  12 88  33 25 五  3  23 64  21

2 个答案:

答案 0 :(得分:0)

您已经为binarySerach完成了代码,唯一缺少的就是从文件中读取它。

  

如何从文件中读取数字?

试试这个

    ....
    List<Integer> list = new ArrayList<Integer>();

    Scanner scanner = new Scanner(new File("resources/abc.txt"));
    while (scanner.hasNextInt()) {
        list.add(scanner.nextInt());
    }

    Integer[] numArray = list.toArray(new Integer[] {});

    Arrays.sort(numArray);
    ....

以下是完整的代码:

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class myBinarySearch {
    public static void main(String[] args) throws Exception {
        int i;
        int target, found;
        // int[] numArray = { 36, 27, 29, 15, 16, 39, 11, 31 };

        List<Integer> list = new ArrayList<Integer>();

        Scanner scanner = new Scanner(new File("resources/abc.txt"));
        while (scanner.hasNextInt()) {
            list.add(scanner.nextInt());
        }

        Integer[] numArray = list.toArray(new Integer[] {});

        Arrays.sort(numArray);
        for (i = 0; i < numArray.length; i++)
            System.out.print(numArray[i] + " ");

        System.out.print("\nEnter the number you are searching for: ");
        target = new Scanner(System.in).nextInt();
        found = theBinarySearch(numArray, target);
        if (found == -1)
            System.out.println("Number not found");
        else
            System.out.println("Number: " + target + " found at position: " + found);
    }

    // Method searching using binary search algorithm
    private static int theBinarySearch(Integer[] numArray, int target) {
        int mid, bottom, top;
        mid = 0;
        bottom = 0;
        top = numArray.length - 1;
        while (top >= bottom) {
            mid = (top + bottom) / 2;
            if (target == numArray[mid])
                break;
            else if (target < numArray[mid])
                top = mid - 1;
            else
                bottom = mid + 1;
        }

        if (target == numArray[mid])
            return mid;
        else
            return -1;
    }

}

答案 1 :(得分:0)

您尚未定义ReadKB。在我的系统上显示:

mohit@mohit:~/somewhere$ javac myBinarySearch.java 
myBinarySearch.java:16: error: cannot find symbol
        target = ReadKb.getInt();
                 ^
  symbol:   variable ReadKb
  location: class myBinarySearch

选择

Scanner in = new Scanner();
and target = in.nextInt();