private class CSCIStudent {
private int studentID; private String name;
private double hoursPerWeekProgramming;
public CSCIStudent ( String name, int id) {...}
public int getID() { return studentID; }
public String getName() { return name; }
public double getHPWP() { return hoursPerWeekProgramming; }
Write a method called studentSort that takes as it argument a List<CSCIStudent> and uses a simple sort mechanism (you may implement either selection, bubble, or insertion sort). The natural order we want to sort on is the number of hours the CSCIStudent spends programming.
List methods you will need to use:
public class
boolean add (T element);
T get(int index);
int size();
Provide your implementation on the next page.
int i = 0;
CSCIStudent temp;
boolean marker = true;
while (marker) {
marker = false;
for(i; i <= list.size() - 1; i++){
if( list.get( i, HPWP) > list.get(i+1, HPWP)) {
temp = list.get(i);
list.get(i) = list.get (i+1);
list.get(i+1) = temp;
marker = true;
}
}
}
Мой вариант
public void studentSort(List<CSCIStudent> list) {
boolean isSorted;
CSCIStudent temp;
while (!isSorted) {
isSorted = true;
for (int i = 0; i < list.size(); i++) {
if (list.get(i - 1) > list.get(i)) {
temp = list.get(i);
list.get(i) = list.get(i - 1);
list.get(i - 1) = temp;
isSorted = false;
}
}
}
}