Answer:
import java.util.ArrayList;
public class MinToFront {
public static void minToFront(ArrayList < Integer > list){
int miniIndex = 0;
for(int j=1; j<list.size(); j++){
if(list.get(miniIndex) > list.get(j))
miniIndex = j;
}
// moving min to front
int min = list.get(miniIndex);
list.remove(miniIndex);
list.add(0, min);
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(4); //value store in list
list.add(7); //value store in list
list.add(9); //value store in list
list.add(2); //value store in list
list.add(7); //value store in list
list.add(7); //value store in list
list.add(5); //value store in list
list.add(3); //value store in list
list.add(5); //value store in list
list.add(1); //value store in list
list.add(7); //value store in list
list.add(8); //value store in list
list.add(6); //value store in list
list.add(7); //value store in list
System.out.println(list);
minToFront(list);
System.out.println(list);
}
}
Output:
[1, 4, 7, 9, 2, 7, 7, 5, 3, 5, 7, 8, 6, 7]
Explanation:
Firstly,we define a method minToFront(), than set an integer variable "miniIndex" to 0, than set a for loop and apply the following condition and inside the loop we apply the if condition, set an integer variable "min" in which we store the value and than we set the following list [4, 7, 9, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7], after that print the list and than print an output.