【问题标题】:Attempting a Binary Search on a Object Array [comparator]尝试对对象数组进行二分搜索 [比较器]
【发布时间】:2020-04-25 19:09:52
【问题描述】:

我这几天一直在努力尝试编写这段代码。基本上,我们必须根据学生数组中可比较的“学生”对象的 SSN 执行二进制搜索。在 SSN 上执行 binarySearch 后,与该 SSN 的名字和姓氏相关联的学生应该打印出来,并且该学生的位置/位置应该打印出来。我遇到的问题是,当我执行 binarySearch 来查找学生的位置/位置时,它总是返回“-1”而不是学生的元素位置。有什么帮助吗?

学生班

package binarySearch;

public class Student implements Comparable<Student>{

    private String firstName, lastName, SSN, bankAccount;



    public Student(String first, String last, String ssn, String bkacct) {

        this.firstName = first;
        this.lastName = last;
        this.SSN = ssn;
        this.bankAccount = bkacct;

    }   

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }


    public String getSSN() {
        return SSN;
    }

    public String getBankAccount() {
        return bankAccount;
    }



    //toString method
    public String toString() {
        return "Employee: [FirstName = " + firstName + ", LastName = " + lastName + ", SSN = " + SSN + ", BankAccount = "
                    + bankAccount + "]";
        }

    public boolean equals(Object other) {

        return (lastName.equals(((Student)other).getLastName()) &&
                firstName.equals(((Student)other).getFirstName())&&
                SSN.equals(((Student)other).getSSN()) && 
                bankAccount.equals(((Student)other).getBankAccount()));
        }


    //Sorting the array based on SSN
    public int compareTo(Student key) {

        return SSN.compareTo(key.getSSN());



    }

}

我在哪里为 binarySearch 排序我的数组

package binarySearch;

public class ObjectBubbleSorter {
    public static void bubbleSort(Comparable[] array) {

        int lastPos;
        int index;
        Comparable temp;

        for(lastPos = array.length-1; lastPos >= 0; lastPos -= 1) {
            for(index = 0; index <= lastPos - 1; index+=1) {

                if(array[index].compareTo(array[index+1]) > 0) {

                    temp = array[index];
                    array[index] = array[index+1];
                    array[index+1] = temp;

                }       
            }

        }

    }

}

以及我在哪里执行二进制搜索

package binarySearch;


public class ObjectBubbleSortTest {

    public static int binarySearch(Student list[], Student key) {

        int low = 0;
        int high = list.length - 1;
        int middle = (low + high + 1)/2;
        int location = -1;

        while((low <= high) && (location == -1)){

            if (list[middle].equals(key)) { //location current middle

                location = middle;

            }
            else if(list[middle].compareTo(key) < 0 ) { //middle too high
                high = middle - 1;
            }

            else {
                low = middle + 1;
            }

            middle = (low + high + 1)/2;
        }

        return location;
    }


    public static void main(String[]args) {


        Student[] student = new Student[5];

        //order: First Name, Last Name, SSN, Bank_Account_Number 
        student[0] = new Student("Adam", "Sarrone", "1234567", "9022345"); 
        student[1] = new Student("Ryan", "Petrowvoksi", "4345123", "0120345"); 
        student[2] = new Student("Jenn", "Henderson", "8124512", "564214"); 
        student[3] = new Student("Ricky", "Jean", "3512345", "612345");
        student[4] = new Student("Dare", "Ogun", "421451", "198213");

        System.out.println("Original array order: \n");
        for (Student element : student) 
            System.out.print(element + "\n");


        //sorting array
        ObjectBubbleSorter.bubbleSort(student);

        System.out.println();

        System.out.println("\nSorted array order: \n");
        for (Student element : student) 
            System.out.print(element + "\n");

        System.out.println();

        //creating student obj
        Student student1 = new Student("Ryan", "Petrowvoksi", "4345123", "0120345"); 


        int studentSSN = binarySearch(student, student1);
        System.out.print(studentSSN);

        System.out.print(student1.getFirstName() + " " + student1.getLastName() + " was found at position: " + studentSSN);

    }

当我执行 binarySearch 时,它总是返回 -1 而不是学生元素位置

【问题讨论】:

    标签: java binary comparator binary-search comparable


    【解决方案1】:

    binarySearch while 循环内将这一行 list[middle].compareTo(key) &lt; 0 更改为 list[middle].compareTo(key) &gt; 0

    您的compareTo 功能似乎与您希望的相反。

    顺便说一句,我建议你把你的 binarySearch 改成这样,更易读:

    public static int binarySearch(Student list[], Student key) {
            int low = 0;
            int high = list.length - 1;
            int middle;
            int location = -1;
    
            while (low <= high) {
                middle = (low + high + 1) / 2;
                int compare = list[middle].compareTo(key);
                if (compare == 0) {
                    location = middle;
                    return location;
                } else if (compare > 0) {
                    high = middle - 1;
                } else {
                    low = middle + 1;
                }
            }
            return location;
    }
    

    【讨论】:

    • 非常感谢!!不敢相信我犯了这个简单的错误,但感谢您指出。祝你有美好的一天。
    • 您能否将您的问题标记为已解决:) 这对我有帮助...谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-10-02
    • 2014-06-21
    • 2013-12-05
    • 1970-01-01
    • 2012-09-16
    • 2010-11-11
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多