简体   繁体   English

插入排序与对象数组?

[英]Insertion sort with an array of objects?

I have an array of Faculty members and I want to use the Insertion Sort on it. 我有很多教职员工,我想在上面使用插入排序。 I get an error at: 我在以下地方收到错误消息:

InsertionSort.insertionSortA(faculty, faculty.length);

The error says : "Method insertionSortA in class InsertionSort cannot be applied to given types; 错误消息:“ InsertionSort类中的方法insertSortA无法应用于给定类型;
required: Comparable [], int 必需:可比[],整型
found: Faculty [], int 找到:教师[],int
I know that doing this will not work: 我知道这样做是行不通的:

InsertionSort.insertionSortA((Comparable[]) faculty, faculty.length);

I know that if I had an Integer[] array would work, but I'm confused why my Faculty[] would not work? 我知道如果我有一个Integer []数组可以工作,但是我很困惑为什么我的Faculty []无法工作?

public class Tester {

public static void main(String[] args) {

    InsertionSort insertionSort = new InsertionSort();
    Education edu = new Education ("BA", "Business", 1);
    Education edu2 = new Education ("BA", "Health Science", 1);
    Education edu3 = new Education ("BA", "Computer Science", 1);

    Faculty[] faculty  = new Faculty[] { 

    new Faculty ("538", "Doe", "Jane", 'M', 1994,1,10, 
     "Assistant", edu),
    new Faculty ("238", "Do", "John", 'F', 1994,6,1, 
     "Assistant", edu2),
    new Faculty ("080", "White", "Snow", 'F', 1994,4,22, 
     "Full", edu3)
    };

    InsertionSort.insertionSortA(faculty, faculty.length);
}

} }

public class InsertionSort {

public static void insertionSortA(Comparable[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
        Comparable nextItem = theArray[unsorted];
        int loc = unsorted;
        while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) > 0)) {
            theArray[loc] = theArray[loc-1];
            loc--;
        } // end while
        theArray[loc] = nextItem;
    } // end for
} // end insertionSort
public static void insertionSortB(Comparable[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
        Comparable nextItem = theArray[unsorted];
        int loc = unsorted;
        while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) < 0)) {
            theArray[loc] = theArray[loc-1];
            loc--;
        } // end while
        theArray[loc] = nextItem;
    } // end for
} 
}

In order to apply insertion sort on your array/collection the elements need to be comparable (Comparison logic is on the basis of how you want to compare 2 faculty members, can be on the basis of name, age, salary etc). 为了在数组/集合上应用插入排序,元素需要具有可比性(比较逻辑基于您要比较2个教职人员的方式,可以基于姓名,年龄,薪水等)。 This can be done by implementing the interface Comparable in Faculty class and defining the function compareTo() as follows : 这可以通过在Faculty类中实现接口Comparable并定义如下的compareTo()函数来完成:

public class Faculty implements Comparable<Faculty>
{
   public int compareTo(Faculty f)
   {

   // comparison logic is on the basis of how you want to compare 2 faculty members 
   //  you might want to compare name, salaries etc

   }
}

compareTo() function must return the following : compareTo()函数必须返回以下内容:

 zero             : If object is same as the specified object (f)
 positive integer : If object is greater than the specified object (f)
 negative integer : If object is less than the specified object (f)

Refer the documentation at the following link 请参阅以下链接中的文档

Without seeing the Faculty class, we can only guess at your problem. 没有看到Faculty班,我们只能猜测您的问题。

It seems that Faculty does not implement Comparable . 看来Faculty没有实现Comparable You can call the function with an Integer[] , because Integer is comparable--it does implement Comparable . 您可以使用Integer[]调用该函数,因为Integer 具有可比性-它确实实现了Comparable

You'll need to implement Comparable<Faculty> in your Faculty class, by overriding compareTo(Faculty) 您需要通过重写compareTo(Faculty)在您的Faculty类中实现Comparable<Faculty>

public int compareTo(Faculty faculty)  {
    //...
}

As far as what this should return, you should review its API . 至于返回的内容,您应该查看其API But in general, if they're exactly equal, return 0 , if this is greater than faculty (whatever you define "greater" to mean), it should return something greater than 0. There aren't any strict rules beyond this, aside from it always returning a consistent value. 但通常,如果它们完全相等,则返回0 ,如果this值大于faculty (无论将“更大”定义为什么意思),它都应返回大于0的值。除此以外,没有任何严格的规则从中总是返回一个一致的值。

For those still stumbling across this page, an alternative solution would be to disregard the Comparable interface. 对于仍在该页面上绊脚的人,一种替代解决方案是忽略Comparable接口。 Since there are a lot of situations when an object could be sorted by more than one attribute, eg, id, first name, last name, or start year. 由于在很多情况下,一个对象可以按多个属性(例如,id,名字,姓氏或开始年份)进行排序。 In this case, you would create separate methods for each sort attribute in the InsertionSort class. 在这种情况下,您将为InsertionSort类中的每个sort属性创建单独的方法。 This would also require you to change the sort methods input parameter type from Comparable[] to Faculty[]. 这还需要您将排序方法输入参数类型从Comparable []更改为Faculty []。

public class InsertionSort {

  public static void byLastName(Faculty[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
      Faculty nextItem = theArray[unsorted];
      int loc = unsorted;
      while ((loc > 0) &&(theArray[loc-1].getLastName().compareTo(nextItem.getLastName()) > 0)) {
        theArray[loc] = theArray[loc-1];
        loc--;
      } 
      theArray[loc] = nextItem;
    }
  }

  public static void byFirstName(Faculty[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
      Faculty nextItem = theArray[unsorted];
      int loc = unsorted;
      while ((loc > 0) &&(theArray[loc-1].getFirstName().compareTo(nextItem.getFirstName()) > 0)) {
        theArray[loc] = theArray[loc-1];
        loc--;
      } 
      theArray[loc] = nextItem;
    }
  }
}

You could then sort your Faculty array like this: 然后,您可以像这样对Faculty数组进行排序:

InsertionSort.byLastName(faculty, faculty.length);

OR 要么

InsertionSort.byFirstName(faculty, faculty.length);

For a more detailed explanation on sorting objects with Insertion Sort check out my blogpost . 有关使用插入排序对对象进行排序的更详细说明,请参阅我的博客文章 It has implementations in Java, C++, Python, and Javascript. 它具有Java,C ++,Python和Javascript的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM