我如何在获取元素编号的同时找到数组中的最小编号

时间:2018-12-13 11:27:17

标签: java arrays sorting

我有一个困扰我的问题。在数组中找到最小的数字很容易,但这是我的问题。当您在数组中找到最小的数字时,最小的数字成为数组的第 a [0] 个元素。 我想要的是,我想找到最小的数字,以及它是哪个元素。像这样

a[0]   a[1]   a[2]   a[3]
 5      20     1      12

我希望我的代码写1 is the smallest number and it's a[2] element of array

我只想从那里获取 2 ,这样我就可以在其余的代码中使用它。任何帮助都感激不尽。谢谢你们。

编辑:我以前尝试过这种方式

int[] dizi = {5 , 12, 20, 1};
    int gecici;


    for (int i=0; i<4; i++) {
        for (int y = 1; y<4; y++) {

            if (dizi[i] > dizi[y]) {

                gecici = dizi[i];
                dizi[i] = dizi[y];
                dizi[y] = gecici;

            }

        }

    }

5 个答案:

答案 0 :(得分:3)

在这种情况下,您可以利用exit()

IntStream.range

示例:

IntStream.range(0, arr.length)
         .mapToObj(index -> new SimpleEntry<>(index, arr[index]))
         .min(Comparator.comparingInt(SimpleEntry::getValue));

答案 1 :(得分:1)

int[] arr = {3,66,22,44,55};
int small=arr[0];
int index=0;
for(int i=0;i<arr.length;i++){
     if(arr[i]<small){
            small = arr[i];
            index = i;
     }
}

答案 2 :(得分:1)

尝试一下。

int[] a = {5, 20, 1, 12};
IntStream.range(0, a.length)
    .mapToObj(i -> i)
    .min(Comparator.comparing(i -> a[i]))
    .ifPresent(i -> System.out.printf(
        "%d is the smallest number and it's a[%d] element of array%n", a[i], i));

如果您的数组为double,则

double[] a = {5, 20, 1, 12};
IntStream.range(0, a.length)
    .mapToObj(i -> i)
    .min(Comparator.comparing(i -> a[i]))
        .ifPresent(i -> System.out.printf(
            "%f is the smallest number and it's a[%d] element of array%n", a[i], i));

您可以通过一种方法来实现。

/**
 * Returns min index of array x.
 * (Returns -1 when length of array x is zero)
 */
static int findMinIndex(int[] x) {
    return IntStream.range(0, x.length)
        .mapToObj(i -> i)
        .min(Comparator.comparing(i -> x[i]))
        .orElse(-1);
}

像这样打电话。

int[] a = {5, 20, 1, 12};
int minIndex = findMinIndex(a);
System.out.printf("%d is the smallest number and it's a[%d] element of arraay%n",
    a[minIndex], minIndex);

答案 3 :(得分:1)

int [] array = {0,1,2,3,4,5,6,7,8,9,10};
int smallestNum=array[0];
int smallestIndex=0;
for(int i=1;i<array[i];i++){
   if(array[i] < smallestNum){ //if you want the last small number then use `<=` (if small number occur multiple times)
      smallestNum = array[i];
      smallestIndex=i;
   }
}

答案 4 :(得分:1)

如果要查找从小到大的每个元素及其相应的索引,则可以执行以下操作:

public class Test {
    public static class Pair implements Comparable<Pair> {
        int value;
        int index;
        public Pair(int _value, int _index) {
            this.value = _value;
            this.index = _index;
        }
        @Override
        public int compareTo(Pair that) {
            return Integer.valueOf(this.value).compareTo(Integer.valueOf(that.value));
        }
    }

    public static void main(String args[]) {
        int[] a =new int[]{5, 20, 1, 12};
        int n = a.length;
        Pair[] p = new Pair[n];
        for (int i = 0; i < n; ++i) p[i] = new Pair(a[i], i);
        Arrays.sort(p);
        for (int i = 0; i < n; ++i) {
            System.out.println(i + "th minimum is "+ p[i].value +" and is located at index "+ p[i].index);
        }
    }
}

上述方法的复杂度将为时间复杂度O(n log n)。但是,如果您只需要知道最小索引及其索引,则可以按照O(n)的时间复杂度轻松地检索它,如下所示:

   int[] a =new int[]{5, 20, 1, 12};
    int n = a.length;
    int minValue = Integer.MAX_VALUE, minIndex = 0;
    for (int i = 0; i < n; ++i) {
        if (minValue > a[i]) {
            minValue = a[i];
            minIndex = i;
        }
    }
    System.out.println("Minimum value is : "+ minValue+ " and it is located at index: "+ minIndex);