187k views
20 votes
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.

User Willis
by
5.0k points

1 Answer

12 votes

Answer:

Step-by-step explanation:

The following code is written in Java and like asked in the question, it is a function that takes in an ArrayList of Integers as a parameter and uses selection sort to sort the array.

public static void selectSort(ArrayList<Integer> list) {

for (int i = 0; i < list.size() - 1; i++)

{

int index = i;

for (int j = i + 1; j < list.size(); j++){

if (list.get(j) < list.get(index)){

index = j;

}

}

int smallerNumber = list.get(index);

list.set(index, list.get(i));

list.set(i, smallerNumber);

}

}

User Aarjav
by
5.3k points