Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二进制搜索相同的项目&;存储并显示它们_Java_Algorithm_Binary Search_Processing Efficiency_Coding Efficiency - Fatal编程技术网

Java 二进制搜索相同的项目&;存储并显示它们

Java 二进制搜索相同的项目&;存储并显示它们,java,algorithm,binary-search,processing-efficiency,coding-efficiency,Java,Algorithm,Binary Search,Processing Efficiency,Coding Efficiency,这是我使用二进制和分离函数搜索多个项目的主要算法。我需要有人重构我的方法 private int Multisearch(TYPE key){ int ind = binarySearchComparator(key); // If element is not present if (ind == -1) return -1; // Count elements on left side.

这是我使用二进制和分离函数搜索多个项目的主要算法。我需要有人重构我的方法

private int Multisearch(TYPE key){

        int ind = binarySearchComparator(key);


     // If element is not present 
        if (ind == -1) 
            return -1; 

        // Count elements on left side. 
        int count = 1; 
        int left = ind - 1; 
        int i = 1;
        while (left >= 0 && (comparator.compare(list[left], key)) == 0) 
        { 
            lol[i] = list[left];      // store left found elements in array here  
            i++;
            count++; 
            left--; 

        } 

        // Count elements  
        // on right side. 
        int right = ind + 1; 

        try{


        while (right < list.length && (comparator.compare(list[right], key)) == 0) 
        { 
            lol[i] = list[right];      // store right found elements in array here  
            i++;
            count++; 
            right++; 



        } 

        }catch(Exception e){

        }


        return count; 
    }



private int binarySearchComparator(TYPE key) {
        int low = 0;
        int high = count - 1;

        while (low <= high) {

            int mid = (low + high) / 2 ;
            TYPE midVal = list[mid];
            int cmp = comparator.compare(midVal, key);
            if (cmp < 0)
                low = mid + 1;
            else if (cmp > 0)
                high = mid - 1;
            else
                return mid; // key found
        }

        return -1;  // key not found.
    }
学生班

public class MobileSearch implements Comparator<Student>{


    private String type;

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }




    @Override
    public int compare(Student student1, Student student2) {


        int result = 0;



        if(this.type.equals("mobile")){
            result = student1.getMobileNo().compareTo(student2.getMobileNo());
            System.out.print(result+"mobile");
        }

        else if(this.type.equals("name")){
            result = student1.getName().getFullName().compareTo(student2.getName().getFullName());
             System.out.println(result+"name");
        }

        else if(this.type.equals("group")){

                 result = student1.getGroup().compareTo(student2.getGroup());
                 System.out.print(result+"group");

             }



        return result;
    }

}
public class Name {

    // Data Types

    private String firstName;
    private String lastName;


    // Constructors

    public Name() {
    }

    public Name(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // setter

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    // getter

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getFullName(){
        return firstName + " " + lastName;
    }

    @Override
    public String toString() {
        return "Name{" + "firstName=" + firstName + ", lastName=" + lastName + '}';
    }







}
public class Student implements Comparable<Student>{


   // Data Types 

   private String studID; 
   private Name name;
   private String gender;
   private String icNo;
   private String mobileNo;
   private Course course;
   private String group;
   private String dOB;


   // Constructors

   public Student() {
   }


    public Student(String studID, Name name, String gender, String icNo, String mobileNo, Course course, String group, String dOB) {
        this.studID = studID;
        this.name = name;
        this.gender = gender;
        this.icNo = icNo;
        this.mobileNo = mobileNo;
        this.course = course;
        this.group = group;
        this.dOB = dOB;
    }



   public Student(Name name) {
        this.name = name;
    }


   // setter


    public void setStudID(String studID) {
        this.studID = studID;
    }

   public void setName(Name name) {
        this.name = name;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setIcNo(String icNo) {
        this.icNo = icNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public void setdOB(String dOB) {
        this.dOB = dOB;
    }


    // getter

    public String getStudID() {
        return studID;
    }

    public Name getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public String getIcNo() {
        return icNo;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public Course getCourse() {
        return course;
    }

    public String getGroup() {
        return group;
    }

    public String getdOB() {
        return dOB;
    }

    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", gender=" + gender + ", icNo=" + icNo + ", mobileNo=" + mobileNo + ", course=" + course + ", group=" + group + ", dOB=" + dOB + '}';
    }


   @Override
   public int compareTo(Student object) { // Sort according to name if name same then sort according to gender and so on.

    int c = this.name.getFullName().compareTo(object.getName().getFullName());

    if(c == 0)
        c = this.gender.compareTo(object.getGender()); 

    if(c == 0)
        c = this.icNo.compareTo(object.getIcNo());  

    if(c == 0)
        c = this.mobileNo.compareTo(object.getMobileNo());

    if(c == 0)
        c = this.group.compareTo(object.getGroup());

    if(c == 0)
        c = this.dOB.compareTo(object.getdOB());

    return c;

  }

   public static Student[] sort(Student[] object,String category){


       Student[] array;

       if(category.equals("ID")){
         for (int i=1; i < object.length; i++) {
           for(int j = 0 ; j < object.length - i ; j++)
               if( (object[j].getGender().compareTo(object[j+1].getGender())) > 0 ){
                    Student lol = object[j];
                    object[j] = object[j+1];
                    object[j+1] = lol;
              }
       }



   }
        array = object;
       return array;
  }


}
public class Course {


    // Data Types

    private String courseCode;
    private String courseName;
    private double courseFee;


    // Constructors

    public Course() {
    }

    public Course(String courseCode, String courseName, double courseFee) {
        this.courseCode = courseCode;
        this.courseName = courseName;
        this.courseFee = courseFee;
    }

    // setter

    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public void setCourseFee(double courseFee) {
        this.courseFee = courseFee;
    }


    // getter

    public String getCourseCode() {
        return courseCode;
    }

    public String getCourseName() {
        return courseName;
    }

    public double getCourseFee() {
        return courseFee;
    }

    @Override
    public String toString() {
        return "CourseCode = " + courseCode + "Course Name = " + courseName + "Course Fee = " + courseFee;
    }




}

如果您的数据已经排序,则不会比二进制搜索更快。如果有多个匹配项,则查找一个匹配项,然后向前(直到找不到更多匹配项)和向后(直到找不到更多匹配项)迭代以获取所有匹配项。(或者,如果您只需要第一个和最后一个匹配的索引,您可以通过再次对第一个/最后一个匹配的索引进行二进制搜索来替换迭代部分。这将使复杂性降低到O(n),无论有多少个匹配)

这需要O(logn)+O(#匹配)


如果这对您来说不够快,请尝试分析您的实现,以找到进一步优化尝试最有意义的瓶颈。

您确定我的方法实际上是O(log n)?我实际上是在做你在我上面的方法中所做的事情。我用二进制搜索函数找到了mid,并使用另一个单独的函数从mid中找到左元素,从mid中找到右元素进行匹配。想象一下,如果我有大量数据,比如说1000个数据,我需要从mid lol左右遍历1k个。This相当于O(N)不是吗?或者可能是O(logn^2)?你是说我的方法是O(logn)+O(N)??那么最终的值是什么?@Cash:如果N是N的阶数,算法就变成O(N),因为结果包含O(N)个元素。如果N低于一个常数,算法就是O(logn)@Cash:如果您不确定您的实现是否具有预期的复杂性,则需要为不同的n值分析其性能。
public class Student implements Comparable<Student>{


   // Data Types 

   private String studID; 
   private Name name;
   private String gender;
   private String icNo;
   private String mobileNo;
   private Course course;
   private String group;
   private String dOB;


   // Constructors

   public Student() {
   }


    public Student(String studID, Name name, String gender, String icNo, String mobileNo, Course course, String group, String dOB) {
        this.studID = studID;
        this.name = name;
        this.gender = gender;
        this.icNo = icNo;
        this.mobileNo = mobileNo;
        this.course = course;
        this.group = group;
        this.dOB = dOB;
    }



   public Student(Name name) {
        this.name = name;
    }


   // setter


    public void setStudID(String studID) {
        this.studID = studID;
    }

   public void setName(Name name) {
        this.name = name;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setIcNo(String icNo) {
        this.icNo = icNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public void setdOB(String dOB) {
        this.dOB = dOB;
    }


    // getter

    public String getStudID() {
        return studID;
    }

    public Name getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public String getIcNo() {
        return icNo;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public Course getCourse() {
        return course;
    }

    public String getGroup() {
        return group;
    }

    public String getdOB() {
        return dOB;
    }

    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", gender=" + gender + ", icNo=" + icNo + ", mobileNo=" + mobileNo + ", course=" + course + ", group=" + group + ", dOB=" + dOB + '}';
    }


   @Override
   public int compareTo(Student object) { // Sort according to name if name same then sort according to gender and so on.

    int c = this.name.getFullName().compareTo(object.getName().getFullName());

    if(c == 0)
        c = this.gender.compareTo(object.getGender()); 

    if(c == 0)
        c = this.icNo.compareTo(object.getIcNo());  

    if(c == 0)
        c = this.mobileNo.compareTo(object.getMobileNo());

    if(c == 0)
        c = this.group.compareTo(object.getGroup());

    if(c == 0)
        c = this.dOB.compareTo(object.getdOB());

    return c;

  }

   public static Student[] sort(Student[] object,String category){


       Student[] array;

       if(category.equals("ID")){
         for (int i=1; i < object.length; i++) {
           for(int j = 0 ; j < object.length - i ; j++)
               if( (object[j].getGender().compareTo(object[j+1].getGender())) > 0 ){
                    Student lol = object[j];
                    object[j] = object[j+1];
                    object[j+1] = lol;
              }
       }



   }
        array = object;
       return array;
  }


}
public class Course {


    // Data Types

    private String courseCode;
    private String courseName;
    private double courseFee;


    // Constructors

    public Course() {
    }

    public Course(String courseCode, String courseName, double courseFee) {
        this.courseCode = courseCode;
        this.courseName = courseName;
        this.courseFee = courseFee;
    }

    // setter

    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public void setCourseFee(double courseFee) {
        this.courseFee = courseFee;
    }


    // getter

    public String getCourseCode() {
        return courseCode;
    }

    public String getCourseName() {
        return courseName;
    }

    public double getCourseFee() {
        return courseFee;
    }

    @Override
    public String toString() {
        return "CourseCode = " + courseCode + "Course Name = " + courseName + "Course Fee = " + courseFee;
    }




}