Question: JAVA I got a function that sorts an array of cars (carLot) by MPG. Can someone modify so that it can sort the array of
JAVA
I got a function that sorts an array of cars (carLot) by MPG. Can someone modify so that it can sort the array of cars by MPG from lowest to highest instead.
private void selectionSort(ArrayListlist) { for (int i = 0; i < list.size() - 1; i++) { // Find the minimum in the list[i..list.length-1] Car currentMax = list.get(i); int currentMaxIndex = i; for (int j = i + 1; j < list.size(); j++) { if (currentMax.getMPG() < list.get(j).getMPG()) { currentMax = list.get(j); currentMaxIndex = j; } } // Swap list[i] with list[currentMinIndex] if necessary; if (currentMaxIndex != i) { list.set(currentMaxIndex, list.get(i)); } list.set(i, currentMax); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
