if I have ArrayList of [5,9,4,2,8,12] how can i get output as [2,5,9,4,8,12]. I could find the minimum but I cannot get the above output.
public static ArrayList<Integer> minToFront(ArrayList<Integer> list) {
int min = Integer.MAX_VALUE;
int first = list.get(0);
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) < min) {
min = list.get(i);
}
list.set(0, min);
}
return list;
}
CodePudding user response:
It seems you want to sort your ArrayList. Though there are more efficient ways to do this, let's start by fixing your method, minToFront()
.
Using a similar approach, we can store a "default" value of the min
and minInd
(minimum index) to be the first value in our list:
int min = list.get(0);
int minInd = 0;
Next, we can iterate through our list and find the minimum element. We do this by finding elements smaller than min
, and if we find an element that is smaller, then we redefine the values of min
and minInd
:
for (int i = 0; i < list.size(); i ) {
if (list.get(i) < min) {
minInd = i;
min = list.get(i);
}
}
Now that we know what the minimum element of the list is, we need to swap that value with our first element. We do this by creating a temporary variable that stores our first element, then swap:
int temp = list.set(0, min);
list.set(minInd,temp);
return list;
In our main method, we can sort the whole list by first defining another list that will store our sorted list.
While our old list still has elements, we can find the minimum element in our old list, move it to our new list, delete it from our old list, and then repeat this process until we have moved all our elements to our new list:
while(origList.size() > 0){
origList = minToFront(origList);
sortedList.add(origList.get(0));
origList.remove(0);
}
After applying all these changes, our code should look something like this:
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList<Integer> sortedList = new ArrayList<Integer>();
ArrayList<Integer> origList = new ArrayList<Integer>();
origList.add(5);
origList.add(9);
origList.add(4);
origList.add(2);
origList.add(8);
origList.add(12);
while(origList.size() > 0){
origList = minToFront(origList);
sortedList.add(origList.get(0));
origList.remove(0);
}
System.out.println(sortedList);
}
public static ArrayList<Integer> minToFront(ArrayList<Integer> list) {
int min = list.get(0);
int minInd = 0;
for (int i = 0; i < list.size(); i ) {
if (list.get(i) < min) {
minInd = i;
min = list.get(i);
}
}
int temp = list.get(0);
list.set(0, min);
list.set(minInd,temp);
return list;
}
}
Output:
[2, 4, 5, 8, 9, 12]
I hope this answered your question! Please let me know if you need any further clarification or details :)
CodePudding user response:
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {5,9,4,2,8,12};
int[] arr2 = new int[arr.length];
int index = 0;
for(int i=1;i<arr.length;i )
{
if(arr[i]<arr[i-1])
{
index=i;
}
}
arr2[0]=arr[index];
int counter=1;
for(int i=0;i<arr.length;i )
{
if(index!=i)
{
arr2[counter ]=arr[i];
}
}
System.out.println();
for (int i = 0; i < arr2.length; i ) {
System.out.print(arr2[i] " ");
}
}
}
CodePudding user response:
int minIndex = -1;
int tmp = array[0];
int min = 0;
for (int i = 0; i < array.length; i ) {
if (array[i] < min) {
minIndex = i;
min = array[i];
}
}
array[0] = min;
array[minIndex] = tmp;
return array;
}```