Question: Write a method, public static void selectSort(ArrayList list), which implements a selection sort on the ArrayList of Integer objects list. For example, if the parameter

Write a method, public static void selectSort(ArrayList list), which implements a selection sort on the ArrayList of Integer objects list. For example, if the parameter list would be printed as [3, 7, 2, 9, 1, 7] before a call to selectSort, it would be printed as [1, 2, 3, 7, 7, 9] after the method call.

----------------------------------------------------------------------------------------

This is what I have so far:

public static void selectSort(ArrayList list) { for (int i = 0; i < list.size() - 1; i++) { // find position of smallest num between (i + 1)th element and last element int index = i; for (int j = i + 1; j < list.size(); j++) { if (list.get(j) < list.get(index)) { index = j; } // Swap min (smallest num) to current position on array int min = list.get(index); list.set(index, list.get(i)); list.set(i, min); } } for (int nums : list) { System.out.print(nums); } }

--------------------------------------------------------------------------------------

When I run my code, I get the following error:

DESCRIPTION This test case checks that your method sorts the ArrayList [3, 7, 2, 9, 1, 7] into ascending order.

MESSAGE Check that your method sorts the list [3, 7, 2, 9, 1, 7] so that after the method call it prints as [1, 2, 3, 7, 7, 9].

I'm not sure what I'm doing wrong. Please help, thank you.

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!