SelectionSort by Alphabetical Order不排序最后一个对象

时间:2015-03-29 08:51:10

标签: java arrays object netbeans selection-sort

我有一个selection sort方法,可以按lastName按升序对一组对象进行排序。在10个数组对象中,有9个被很好地排序。它只是最后一个没有排序的对象。


测试数组对象的声明:

int studentIndex = 8;
Student[] studentList = new Student[10];
studentList[0] = new Student();
studentList[1] = new Student();
studentList[2] = new Student();
studentList[3] = new Student();
studentList[4] = new Student();
studentList[5] = new Student();
studentList[6] = new Student();
studentList[7] = new Student();
studentList[8] = new Student();
studentList[9] = new Student();
studentList[0].setStudent("Mr", "Jason", "Lee", 1, 1, 1, 2001, 85, 75, 8, 65, 72.5, "D");
studentList[1].setStudent("Miss", "Candice", "Teo", 2, 2, 02, 2002, 95, 95, 9, 90, 92.0, "HD");
studentList[2].setStudent("Mrs", "Sophie", "Chua", 3, 3, 03, 2003, 65, 78, 7, 92, 81.6, "HD");
studentList[3].setStudent("Mrs", "Brendon", "Low", 4, 4, 04, 2004, 95, 98, 7, 69, 80.1, "HD");
studentList[4].setStudent("Mr", "Clarance", "Yeo", 5, 5, 05, 2005, 80, 76, 5, 59, 65.7, "C");
studentList[5].setStudent("Mr", "Adrian", "Tan", 6, 6, 6, 2006, 70, 60, 4, 20, 40.0, "ND");
studentList[6].setStudent("Ms", "Debbie", "Toh", 7, 7, 7, 2007, 60, 60, 7, 50, 56.0, "P");
studentList[7].setStudent("Miss", "Sarah", "Ho", 8, 8, 8, 2008, 59, 61, 7, 62, 62.0, "N");
studentList[8].setStudent("Mr", "Aloysius", "Lim", 9, 9, 9, 2009, 62, 63, 8, 69, 67.5, "C");
studentList[9].setStudent("Mrs", "Peggy", "Chan", 10, 10, 10, 2010, 65, 67, 9, 72, 71.4, "D");

Peggy Chua没有排序,显示为最后一个结果,即使她的姓氏是Chan


主要方法:

SelectionSort(studentList, studentIndex);
    if (studentIndex > 0) {
        for (int i = 0; i <= studentIndex + 1; i++) {
            if (studentList[i] != null) {
                studentList[i].writeOutput();
            } 
                else if (studentIndex == 0) {
                System.out.println("No sorting required");
                    studentList[0].writeOutput();
                }
            }
        } 
    }

SelectionSort方法:

public static void SelectionSort(Student[] st, int index) {
    int i, j, first;
    String jLastName;
    String firstLastName;
    for (i = index; i > 0; i--) {
        first = 0;
        for (j = 1; j <= i; j++) {
            if (st[j] != null) {
                jLastName = st[j].getLastName();
                if (st[first] != null) {
                    firstLastName = st[first].getLastName();
                    if ((jLastName.compareToIgnoreCase(firstLastName)) > 0) {
                        first = j;
                    }
                }
            }
        }
        Student temp2;
        temp2 = st[first];
        st[first] = st[i];
        st[i] = temp2;
    }
}

我不确定导致最后一个对象未​​被排序的错误在哪里。以下是示例输出:

Option: 11
********************************************************************
Student ID: 3
Student Title: Mrs
Student First Name: Sophie
Student Last Name: Chua
Student DOB: 3/3/2003
Assignment 1: 65
Assignment 2: 78
Practical: 7
Exam: 92
Overall: 81.6
Final Grade: HD
********************************************************************
********************************************************************
Student ID: 8
Student Title: Miss
Student First Name: Sarah
Student Last Name: Ho
Student DOB: 8/8/2008
Assignment 1: 59
Assignment 2: 61
Practical: 7
Exam: 62
Overall: 62.0
Final Grade: N
********************************************************************
********************************************************************
Student ID: 1
Student Title: Mr
Student First Name: Jason
Student Last Name: Lee
Student DOB: 1/1/2001
Assignment 1: 85
Assignment 2: 75
Practical: 8
Exam: 65
Overall: 72.5
Final Grade: D
********************************************************************
********************************************************************
Student ID: 9
Student Title: Mr
Student First Name: Aloysius
Student Last Name: Lim
Student DOB: 9/9/2009
Assignment 1: 62
Assignment 2: 63
Practical: 8
Exam: 69
Overall: 67.5
Final Grade: C
********************************************************************
********************************************************************
Student ID: 4
Student Title: Mrs
Student First Name: Brendon
Student Last Name: Low
Student DOB: 4/4/2004
Assignment 1: 95
Assignment 2: 98
Practical: 7
Exam: 69
Overall: 80.1
Final Grade: HD
********************************************************************
********************************************************************
Student ID: 6
Student Title: Mr
Student First Name: Adrian
Student Last Name: Tan
Student DOB: 6/6/2006
Assignment 1: 70
Assignment 2: 60
Practical: 4
Exam: 20
Overall: 40.0
Final Grade: ND
********************************************************************
********************************************************************
Student ID: 2
Student Title: Miss
Student First Name: Candice
Student Last Name: Teo
Student DOB: 2/2/2002
Assignment 1: 95
Assignment 2: 95
Practical: 9
Exam: 90
Overall: 92.0
Final Grade: HD
********************************************************************
********************************************************************
Student ID: 7
Student Title: Ms
Student First Name: Debbie
Student Last Name: Toh
Student DOB: 7/7/2007
Assignment 1: 60
Assignment 2: 60
Practical: 7
Exam: 50
Overall: 56.0
Final Grade: P
********************************************************************
********************************************************************
Student ID: 5
Student Title: Mr
Student First Name: Clarance
Student Last Name: Yeo
Student DOB: 5/5/2005
Assignment 1: 80
Assignment 2: 76
Practical: 5
Exam: 59
Overall: 65.7
Final Grade: C
********************************************************************
********************************************************************
Student ID: 10
Student Title: Mrs
Student First Name: Peggy
Student Last Name: Chan
Student DOB: 10/10/2010
Assignment 1: 65
Assignment 2: 67
Practical: 9
Exam: 72
Overall: 71.4
Final Grade: D
********************************************************************

最后一个对象Peggy Chan未按照输出中lastName的{​​{1}}进行排序。

1 个答案:

答案 0 :(得分:1)

您可以在

处开始排序
studentList[8].setStudent("Mr", "Aloysius", "Lim", 9, 9, 9, 2009, 62, 63, 8, 69, 67.5, "C");

因为studentIndex = 8而你后退到0,这意味着你永远不会评估

studentList[9].setStudent("Mrs", "Peggy", "Chan", 10, 10, 10, 2010, 65, 67, 9, 72, 71.4, "D");

所以,因为陈太太停留在名单的末尾