简体   繁体   English

如何在保留元素顺序的同时将列表的最小值移动到第一个元素

[英]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:使用类似的方法,我们可以将minminInd (最小索引)的“默认”值存储为列表中的第一个值:

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的元素来做到这一点,如果我们找到一个更小的元素,那么我们重新定义minminInd的值:

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.

相关问题 如何在保留原始顺序的同时反转 java 中的链表? - How to Reverse a linked list in java while preserving the original order? 如何使用Java 8和流将属性值从列表的第一个元素传播到所有列表元素? - How to propagate property value from list first element to all list elements using java 8 and streams? 从第一个列表中查找最小元素 - Finding minimum element from first list 对对象列表中的数据进行分组和求和,同时保留插入顺序 - Group and sum data from a list of objects while preserving the insertion order 如何按字母顺序对列表列表的第一个元素进行排序? - How to sort the first elements of List of Lists in alphabetical order? 如何按日期排序列表,然后在第一个位置搜索元素和设置 - How to order a list by date and then search a element and setting at first position 读取用户输入到列表,根据元素值排序,并按顺序返回数组中的列表元素-Java - Read user input to list, sort based on element value, and return list elements within array in order - Java 在保持顺序的同时将元素解组到列表中 - Unmarshalling elements into list while maintaining the order 如何比较列表元素以获得最大值? - how to Compare list element in order to get the maximum value? 如何在保留订单的同时将List <P>中的元素分组到Map <K,List <V >>? - How do you group elements in a List<P> to a Map<K, List<V>> while retaining order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM