- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个选择排序
方法,可以通过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
未按她的 Chua
的 lastName
排序。
最佳答案
您开始排序例程
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/
我有一个选择排序方法,可以通过lastName按升序对对象数组进行排序。在 10 个数组对象中,有 9 个排序良好。这只是最后一个未排序的对象。 测试数组对象的声明: int studentIndex
所以我在测试这段代码时注意到: public class SelectionSort{ public static void sorted(int[] array){ for
我有一个家庭作业,我必须使用以下参数“改进”SelectionSort: 使用“改进的”SelectionSort 对给定列表进行排序 在一次迭代中,找到最小和第二小的元素 将最小的和第二小的元素放在
我是初学者,现在我正在尝试第二天实现 SelectionSort 以进行练习。我使用的算法大部分时间都有效,但并非总是如此。不幸的是,我完全不清楚为什么它并不总是有效。这个例子是一个不起作用的例子。
我应该得到输出为 {-10,6,7,13,20,80,90,100} 但是我得到的输出是 {20,100,13,6,7,80,-10,90} 在选择排序中。为什么我得到这个输出?我该如何解决这个问题?
我正在尝试用 bool 编写 SelectionSort。 void ss( int tab[], int n ) { bool flag; for( int i = 0; i ta
SOF。我有一个问题,我遇到了一些困难。 下面的代码应该逐行筛选文件,有效地利用 StringTokenizer 来获取汽车类的品牌、型号、年份和里程(按顺序)并将它们存储在汽车对象中,该对象然后我添
我正在尝试在我自己编写的双向链表版本中使用选择排序算法。对于这个问题,我们可以假设除了我发布的代码之外,其他地方没有错误(至少没有与问题相关的错误)。我已经做了很多测试。 这是我的方法: public
为什么在实现选择排序时优先使用函数模板而不是将 int 数组传递给函数?如果我们只是想比较整数,为什么我们希望它可以对任何类型的数组进行操作?为什么我们在 swap 函数中使用不同的类型?我们不能再次
我是一名优秀的程序员,十分优秀!