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(ArrayList list) { 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!