【问题标题】:How to find smallest number without using java sort() [closed]如何在不使用 java sort() 的情况下找到最小的数字 [关闭]
【发布时间】:2015-04-22 15:31:53
【问题描述】:

如何在不使用 java sort() 方法和整数数组列表中的排序技术的情况下找到最小的数字?我用Collections.min(),我的sn-p是:

public class SmallestNum {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter size of Array::");
        int sizee=sc.nextInt();
        List<Integer> numbers=new ArrayList<Integer>();
        for(int i=0;i<sizee;i++) {
            numbers.add(sc.nextInt());
        }
        //Collections.sort(numbers);
        System.out.println(" Search value:");
        int num=sc.nextInt();
        Integer mini=Collections.min(numbers);
        System.out.println("Minimum Value is:"+mini);
        for(int i=0;i<sizee;i++) {
            System.out.println("Minimum:"+(mini));
            *if(mini.equals(numbers)) {
                int smallSec=Integer.MAX_VALUE;
            }
            /*System.out.println(" Search value:");
            int num=sc.nextInt();
        }
        System.out.println(numbers.get(num-1));
            */
        }
    }
}

这个程序不工作。请提出替代逻辑。我不明白如果用户想搜索下一个最小的数字怎么办?

【问题讨论】:

  • 如果您的程序无法运行,您会收到任何错误吗?
  • @MadConan codereview 用于代码改进,而不是作为调试代码的平台。
  • if (a > b) then swap(a,b) :: at嵌套for循环
  • if(mini.equals(numbers))IntegerList&lt;Integer&gt; 进行比较。
  • 这部分代码是否需要注释:*if(mini.equals(numbers)) { int smallSec=Integer.MAX_VALUE; } ?好像编译不出来。

标签: java arrays sorting arraylist


【解决方案1】:

Collections.min 会直接给你最小值。您需要进行额外的比较。但是如果你想在不使用开箱即用的方法的情况下找到最小数量,你可以简单地使用这个逻辑

Integer minNum = Integer.MAX_VALUE;
for(Integer i :  numbers){
    if(i < minNum) {
        minNum = i;
    }
}
System.out.println("Minimum Value is: " + minNum);

考虑到您正在阅读“搜索值”,我假设您正在寻找其他内容。请详细说明您要达到的目标。

根据评论更新。

为此,您可以使用quick select algorithm。该程序将是

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SmallestNum {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter size of Array::");
        int size = sc.nextInt();
        List<Integer> numbers = new ArrayList<Integer>();
        for (int i = 0; i < size; i++) {
            numbers.add(sc.nextInt());
        }
        System.out.println(" Search value:");
        int n = sc.nextInt();
        if (n > 0 && n <= size) {
            int nthSmall = select(numbers, 0, size - 1, n);
            System.out.println("Minimum Value is: " + nthSmall);
        } else {
            System.out.println("Invalid Entry");
        }
    }

    private static int select(List<Integer> list, int left, int right, int n) {
        int pivotIndex = getPivotIndex(list, left, right);
        if (pivotIndex == n - 1) {
            return list.get(pivotIndex);
        }
        if ((n - 1) < pivotIndex) {
            return select(list, left, pivotIndex - 1, n);
        } else {
            return select(list, pivotIndex + 1, right, n);
        }

    }

    private static int getPivotIndex(List<Integer> list, int left, int right) {
        int pivot = list.get((left + right) / 2);
        while (left < right) {
            while (list.get(left) < pivot) {
                left++;
            }
            while (list.get(right) > pivot) {
                right--;
            }

            if (left < right) {
                int temp = list.get(left);
                list.set(left, list.get(right));
                list.set(right, temp);
                left++;
                right--;
            }

        }
        return left;
    }
}

【讨论】:

  • 如果数字={0,4,1,5,6,9};这是我们的数组,如果用户输入 3,我们需要在数组中显示第三小的整数 i.t op 将只有 4
  • 使用你的 sn-p 它给出 op Enter size of Array::(5) 6 4 22 1 2 搜索值:2 最小值是:1 最小值:1 最小值是:2 最小值:1最小值是:1 最小值:1 最小值是:1 最小值:1 最小值是:1 最小值:1 最小值是:4
  • /* 这就是我想要的 */
【解决方案2】:

你可以这样做:

   public static void main(String[] args) {
     long delta = System.nanoTime();

     int[] inputArray = new int[]{0,4,1,5,6,9};
     int n = 3;

     System.out.println("the " + n + "th min is " + getNthMin(inputArray, n));
     System.out.println();
  }

  private static int getNthMin(int[] array, int n) {
     int nthMin = Integer.MIN_VALUE;
     int[] tmpArray = Arrays.copyOf(array, array.length);
     for (int i = 0; i < n; i++) {
        int minIndex = 0;
        int min = tmpArray[minIndex];
        for (int j = 1; j < tmpArray.length; j++) {
           if (tmpArray[j] < min) {
              min = tmpArray[j];
              minIndex = j;
           }
        }
        tmpArray[minIndex] = Integer.MAX_VALUE;

        if (min > nthMin) {
           nthMin = min;
        }
     }
     return nthMin;
  }

或者使用列表来迭代更少的元素:

  private static int getNthMin2(Integer[] array, int n) {
     int nthMin = Integer.MIN_VALUE;
     List<Integer> list = new ArrayList<Integer>(Arrays.asList(array));
     for (int i = 0; i < n; i++) {
        Integer min = list.get(0);
        for (Integer value : list) {
           if (value < min) {
              min = value;
           }
        }
        list.remove(min);

        if (min > nthMin) {
           nthMin = min;
        }
     }
     return nthMin;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多