gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:43:23 25 4
gpt4 key购买 nike

我有一个选择排序方法,可以通过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();
}
}
}
}
<小时/>

选择排序方法:

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 未按她的 ChualastName 排序。

最佳答案

您开始排序例程

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");

所以陈太就排在名单的最后

关于java - SelectionSort 按字母顺序排序不排序最后一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29327259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com