142k views
3 votes
Write a method minToFront that takes an ArrayList of integers as a parameter and that moves the minimum value in the list to the front, otherwise preserving the order of the elements. For example, if a variable called list stores the following values: {3, 8, 92, 4, 2, 17, 9} and you make this call: minToFront(list); it should store the following values after the call: {2, 3, 8, 92, 4, 17, 9} You may assume that the list stores at least one value.

User Tage
by
5.8k points

1 Answer

5 votes
The key to this algorithm is to compare each value with its previous value. If the previous value is larger than the current value, change currentVal= previous value and previousVal = currentVal (of course u will need a tempVal to store one of the value). The program should loop from first value til the very end and should only run once.
User Sospedra
by
5.3k points