如何在不使用java sort()的情况下找到最小的数字

时间:2015-04-22 15:31:54

标签: java arrays sorting arraylist

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

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));
            */
        }
    }
}

此程序无效。请建议替代逻辑。我不明白用户是否想搜索下一个最小的号码怎么做?

3 个答案:

答案 0 :(得分:0)

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

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

考虑到您正在阅读&#34;搜索值&#34;我假设你正在寻找别的东西。请详细说明您要实现的目标。

根据评论进行更新。

为此你可以使用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;
    }
}

答案 1 :(得分:0)

你可以这样做:

   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;
  }

答案 2 :(得分:0)

import java.util.TreeSet;

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();
    TreeSet<Integer> numbers = new TreeSet<Integer>();
    for (int i = 0; i < sizee; i++) {
        numbers.add(sc.nextInt());
    }
    System.out.println(" Search value:");
    int num = sc.nextInt();
    System.out.println("Minimum Value is:" + numbers.first());
    System.out.println("Minimum:" + numbers.ceiling(num));//including num
    //alternatively( Idon't know the exact task)
    //System.out.println("Minimum:" + numbers.ceiling(num+1));//excluding num        
    }
}