[英]How to moves the minimum value of the list to be the first element while preserving the order of the elements
if I have ArrayList of [5,9,4,2,8,12] how can i get output as [2,5,9,4,8,12].如果我有 [5,9,4,2,8,12] 的 ArrayList,我怎样才能得到 output 作为 [2,5,9,4,8,12]。 I could find the minimum but I cannot get the above output.
我可以找到最小值,但我找不到上面的 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;
}
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()
.您似乎想要对 ArrayList 进行排序。虽然有更有效的方法可以做到这一点,但让我们从修复您的方法
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:使用类似的方法,我们可以将
min
和minInd
(最小索引)的“默认”值存储为列表中的第一个值:
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
:我们通过找到小于
min
的元素来做到这一点,如果我们找到一个更小的元素,那么我们重新定义min
和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: 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 :)我希望这回答了您的问题:如果您需要任何进一步的说明或详细信息,请告诉我 :)
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] + " ");
}
}
} }
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;
}```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.