对数组进行排序并使用二进制搜索

时间:2015-04-09 06:35:55

标签: java arrays sorting

我已经让这个程序运行了,它几乎给了我想要的答案。在运行整个程序之后,它应该打印出键的索引以及键也相等。我不能为我的生活弄清楚如何将索引放入控制台中的最终消息。例如,如果你有一个{0,1,2,3,4,5,6,7,8,9}的数组并且你的键是0,它应该返回:list [0] =我标记了我认为我的问题所在的部分"(索引???)"

import java.util.Scanner;
public class PartA {
    public static void main(String[] args){
        System.out.println("Please enter 10 double values:");
        double [] array = inputArray();
        selectionSort(array);
        printArray(array);
        System.out.println("Please enter a search key:");
        Scanner input = new Scanner(System.in);
        double key = input.nextDouble();
        double x = binarySearch(array,key);
        if (x != -1)
            System.out.println("list[" + (index???) + "] = " + key);
        else
            System.out.println(key + " is not on the list");
        }

        public static double[] inputArray(){
            Scanner array = new Scanner(System.in);
            double [] list = new double[10];
            for(int i = 0; i < list.length; i++){
                array.hasNextDouble();
                list[i] = array.nextDouble();
            }
            return list;
        }
        public static void selectionSort(double[] list){            
            double array[] = new double [list.length];
            for(int i = 0; i < list.length; i++){
                for(int j = 0; j < list.length; j++){
                    if (list[i] < list[j]){
                        double x = list[i];
                        list[i] = list[j];
                        list[j] = x;
                    }
                }
            }
        }
        public static void printArray(double[] list){
            for (int i = 0; i < list.length; i++){
                System.out.println("list[" + i + "] = " + list[i]);
            }
            System.out.println();
        }
        public static int binarySearch(double []list, double key){
            int low = 0, high = list.length - 1;
            while(high >= low){
            int mid = (low + high)/2;
            if(key < list[mid])
                high = mid - 1;
            else if(key == list[mid])
                return mid;
            else low = mid + 1;
            }
            return -1;
        }
}

2 个答案:

答案 0 :(得分:1)

我也在问题的评论中提到了解决方案,但为了完整起见,我发布了这个答案。

public class Main {
    public static void main(String[] args){
        System.out.println("10 double values:");
        double [] array = {0,1,2,3,4,5,6,7,8,9};
        selectionSort(array);
        printArray(array);
        double key = 4;
        System.out.println("Searched Key: " + key);
        int idx = binarySearch(array,key); //use int instead of double
        if (idx != -1)
            //use the variable identifier to print the index
            System.out.println("list[" + idx + "] = " + key); 
        else
            System.out.println(key + " is not on the list");
        }

        public static void selectionSort(double[] list){            
            double array[] = new double [list.length];
            for(int i = 0; i < list.length; i++){
                for(int j = 0; j < list.length; j++){
                    if (list[i] < list[j]){
                        double x = list[i];
                        list[i] = list[j];
                        list[j] = x;
                    }
                }
            }
        }
        public static void printArray(double[] list){
            for (int i = 0; i < list.length; i++){
                System.out.println("list[" + i + "] = " + list[i]);
            }
            System.out.println();
        }
        public static int binarySearch(double []list, double key){
            int low = 0, high = list.length - 1;
            while(high >= low){
            int mid = (low + high)/2;
            if(key < list[mid])
                high = mid - 1;
            else if(key == list[mid])
                return mid;
            else low = mid + 1;
            }
            return -1;
        }
}

使用Ideone的工作示例: http://ideone.com/WXrJPi

答案 1 :(得分:0)

我认为您的代码非常完美。您只需要用x替换(index ???)并将x的类型从double更改为int,这样就不会将该值打印为double值。

 int x = binarySearch(array,key);
 if (x != -1)
 System.out.println("list[" + (x) + "] = " + key);
 else
 System.out.println(key + " is not on the list");
 }

输出:

Please enter 10 double values:
0
1
2
3
4
5
6
7
8
9
list[0] = 0.0
list[1] = 1.0
list[2] = 2.0
list[3] = 3.0
list[4] = 4.0
list[5] = 5.0
list[6] = 6.0
list[7] = 7.0
list[8] = 8.0
list[9] = 9.0

Please enter a search key:
5
list[5] = 5.0