简体   繁体   English

SelectionSort按字母顺序排序不对最后一个对象排序

[英]SelectionSort by Alphabetical Order not sorting the last object

I have a selection sort method that sorts an array of objects via lastName in ascending order. 我有一个selection sort方法,该方法通过lastName以升序对对象数组进行排序。 Out of 10 array objects, 9 were sorted well. 在10个数组对象中,有9个被很好地排序。 It was only the last object that wasn't sorted. 这只是未排序的最后一个对象。


Declaration of test array objects: 测试数组对象的声明:

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 was not sorted and was displayed as the last result even though her lastName is Chan Peggy Chua未排序,即使她的姓氏是Chan,也被显示为最后结果


Main Method: 主要方法:

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 Method: 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;
    }
}

I am unsure where is the error that caused the last object to be not sorted. 我不确定导致最后一个对象未​​排序的错误在哪里。 Below is a sample output: 以下是示例输出:

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
********************************************************************

The last object Peggy Chan was not sorted by her lastName of Chua in the output. 最后一个对象Peggy Chan不是由她来分类lastNameChua输出。

You start your sorting routine at 您从以下位置开始排序例程

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

because studentIndex = 8 and you go backwards to 0, which means you never evaluate 因为studentIndex = 8而您倒退到0,这意味着您永远不会求值

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

so it makes since that Mrs. Chan stays at the end of the list 因为陈太太一直排在最后

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

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