对存储Car对象的ArrayList执行选择排序出现异常结果的技术求助
Fixing Your Descending MPG Selection Sort in CarLot
Hey Kyle, let's work through that wonky selection sort issue you're hitting with your CarLot class. From the code snippet you shared, it looks like you started the logic but missed a few critical pieces that are causing those unexpected results. Let's break down the problems and fix them step by step.
Common Pitfalls in Your Current Code
From what you've shown, here are the likely culprits:
- Missing nested loop: Selection sort requires an outer loop to iterate through each position, and an inner loop to find the maximum value in the remaining unsorted section. Your code only has a single for-each loop, which can't properly traverse and compare all elements.
- Index handling issues: For-each loops don't give you direct access to element indices, which you need to track the position of the maximum MPG car. The incomplete
lotSortedByMPG.index...call suggests you were trying to get an index, but this approach won't work with a for-each loop. - No element swapping: Even if you found the maximum MPG car, you didn't include logic to swap it into the correct position in the sorted list.
Corrected Selection Sort Implementation
Here's a fixed version of getSortedDescMPG() that addresses all these issues:
public ArrayList<Car> getSortedDescMPG() { // Create a copy of the original list to avoid modifying myCars directly ArrayList<Car> lotSortedByMPG = new ArrayList<>(myCars); int lotSize = lotSortedByMPG.size(); // Outer loop: iterate through each position in the list for (int currentPosition = 0; currentPosition < lotSize - 1; currentPosition++) { // Assume the current position holds the maximum MPG car initially int maxMPGIndex = currentPosition; double maxMPG = lotSortedByMPG.get(currentPosition).getMPG(); // Inner loop: find the car with the highest MPG in the unsorted section for (int j = currentPosition + 1; j < lotSize; j++) { Car currentCar = lotSortedByMPG.get(j); if (currentCar.getMPG() > maxMPG) { maxMPG = currentCar.getMPG(); maxMPGIndex = j; } } // Swap the maximum MPG car into the current sorted position (if needed) if (maxMPGIndex != currentPosition) { Car tempCar = lotSortedByMPG.get(currentPosition); lotSortedByMPG.set(currentPosition, lotSortedByMPG.get(maxMPGIndex)); lotSortedByMPG.set(maxMPGIndex, tempCar); } } return lotSortedByMPG; }
Key Fixes Explained
- Nested loops: The outer loop handles each position we want to fill with the next highest MPG car, while the inner loop scans the remaining unsorted elements to find that maximum.
- Index tracking: Using regular for loops (instead of for-each) lets us directly manage indices, which is essential for finding and swapping elements.
- Safe swapping: We only swap elements if the maximum MPG car isn't already in the current position, avoiding unnecessary operations.
- Preserving original data: By creating a copy of
myCarsat the start, we ensure the original list remains unchanged while returning the sorted version.
Quick Checks to Verify
After implementing this, test with a small sample of Car objects to confirm:
- Cars are ordered from highest MPG to lowest
- The original
myCarslist inCarLotisn't modified - Edge cases (like multiple cars with the same MPG) work as expected
内容的提问来源于stack exchange,提问作者Kyle Murar
相关产品推荐
相关产品推荐

