使用对象数组进行插入排序?

时间:2014-02-25 02:29:25

标签: java arrays sorting object insertion-sort

我有一系列教员,我想在其上使用插入排序。 我收到错误:

InsertionSort.insertionSortA(faculty, faculty.length);

错误说:“InsertionSort类中的方法insertedSortA不能应用于给定类型;
required:Comparable [],int
发现:Faculty [],int
我知道这样做不会起作用:

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

我知道如果我有一个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
} 
}

3 个答案:

答案 0 :(得分:3)

为了对您的数组/集合应用插入排序,元素需要具有可比性(比较逻辑基于您希望如何比较2名教职员,可以基于姓名,年龄,薪水等) 。这可以通过在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()函数必须返回以下内容:

 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)

请参阅以下link

中的文档

答案 1 :(得分:2)

如果没有看到Faculty课程,我们只能猜测您的问题。

Faculty似乎没有实现Comparable。您可以使用Integer[]调用该函数,因为Integer 可比较 - 实现Comparable

您需要通过覆盖Comparable<Faculty>

Faculty课程中实施compareTo(Faculty)
public int compareTo(Faculty faculty)  {
    //...
}

至于应该返回的内容,您应该查看其API。但总的来说,如果它们完全相同,则返回0,如果this大于faculty(无论定义“更大”意味着什么),它应该返回大于0的东西。除此之外没有任何严格的规则,除了它总是返回一致的值。

答案 2 :(得分:0)

对于那些仍然在这个页面上磕磕绊绊的人来说,另一种解决方案就是忽视Comparable界面。由于在很多情况下,对象可以按多个属性排序,例如id,名字,姓氏或开始年份。在这种情况下,您将为InsertionSort类中的每个排序属性创建单独的方法。这还需要您将排序方法输入参数类型从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;
    }
  }
}

然后你可以像这样对你的Faculty数组进行排序:

InsertionSort.byLastName(faculty, faculty.length);

OR

InsertionSort.byFirstName(faculty, faculty.length);

有关使用“插入排序”排序对象的更详细说明,请查看我的blogpost。它具有Java,C ++,Python和Javascript的实现。

相关问题