// done. public interface SortableObject { public abstract double getSortValue()
ID: 3655775 • Letter: #
Question
// done.
public interface SortableObject {
public abstract double getSortValue();
}
//TODO Modify to operate over arrays of SortableObjects
public interface Sorter {
void sort(int[] a);
}
//TODO: Modify to operate over arrays of SortableObjects
public class SelectionSorter implements Sorter {
@Override
public void sort(int[] a) {
for (int i = 0; i < a.length; i++) {
int minIdx = min(a, i);
swap(a, i, minIdx);
}
}
private int min(int[] a, int start) {
// return index of minimum value
// to the "right" of start
int minVal = a[start];
int minIdx = start;
for (int i = start; i < a.length; i++) {
if (a[i] < minVal) {
minVal = a[i];
minIdx = i;
}
}
return minIdx;
}
private void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
Explanation / Answer
// TODO: Modify to operate over arrays of SortableObjects public interface SortableObject { public abstract double getSortValue(); } public class SelectionSorter implements Sorter { @Override public void sort(SortableObject[] a) { for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.