0

I'm given a List<Integer> and I'm trying to return the 2 smallest integers in the List in a new array.

To do this, I have created a helper that finds the smallest number in the array and then used that in my main function, where I hope to use a while loop that runs until 2, since I need the 2 smallest numbers, and remove the first (smallest) number in order to find the next smallest number and then add them to the new array I made.

Here is my code:

public static int countSorthelper(List<Integer> arr) {
    int temp = 0;
    int n = 0;
    while(n <= 2){
   for (int x = 0; x < arr.size(); x++){
       for (int y = x+1; y < arr.size() && y <= x+y; y++){
           if(arr.get(y) > arr.get(x)){
               temp = arr.get(x);
               n++;
           }
       }
   }
   }
   return temp;

}
public static List<Integer> countSort(List<Integer> arr){
    int n = 0;
    List<Integer> j = new ArrayList<>();
    while (n <= 2){
        countSorthelper(arr);
        arr.remove(countSorthelper(arr));
        j.add(countSorthelper(arr));
        n++;
    }
    return j;
}

When I try to run this, the output terminates due to too much running time, what changes do I need to make in my code??

Droid
  • 520
  • 1
  • 7
  • Look at https://stackoverflow.com/questions/20518078/how-to-sort-listinteger, sort your input and get the two smalles values – FredvN Nov 27 '22 at 15:33
  • 1
    Does this answer your question? [Find two smallest numbers using java?](https://stackoverflow.com/questions/5048665/find-two-smallest-numbers-using-java) – tevemadar Nov 27 '22 at 16:13
  • Sort the array ascending and pick first two elements from it. – Christoph Dahlen Nov 27 '22 at 16:15

1 Answers1

0

There is no need to sort as you can do this in linear time. The code should be self explanatory but basically you need to do comparisons against both min1 and min2, exchanging their values as appropriate. You can see this in the output. The values will either be equal, or min1 will be the smallest and min2 the next smallest.

If you want to return them you can return them in a record.

record Minimums(int smallest, int nextSmallest) {}

List<Integer> list = List.of(1,10,2,5,10,-2,22,3,-1);
Minimums mins = findSmallest(list);
System.out.println("\n"+mins);
  • as min1 changes, min1 replaces min2
  • but the next value in the list could be larger than min1 but smaller than min2 so min2 needs to be independently tested.
   
public static Minimums findSmallest(List<Integer> list) {
    int min1 = Integer.MAX_VALUE;
    int min2 = Integer.MAX_VALUE;
    for (int val : list) {
        if (val < min1) {
            min2 = min1;
            min1 = val;

        } else if (val < min2) {
            min2 = val;
        }
        System.out.printf("val = %2d : min1 = %2d,  min2 = %2d\n", val,
                min1, min2);
    }
    return new Minimums(min1, min2);
}

prints

val =  1 : min1 =  1,  min2 = 2147483647
val = 10 : min1 =  1,  min2 = 10
val =  2 : min1 =  1,  min2 =  2
val =  5 : min1 =  1,  min2 =  2
val = 10 : min1 =  1,  min2 =  2
val = -2 : min1 = -2,  min2 =  1
val = 22 : min1 = -2,  min2 =  1
val =  3 : min1 = -2,  min2 =  1
val = -1 : min1 = -2,  min2 = -1

Minimums[smallest=-2, nextSmallest=-1]


WJS
  • 36,363
  • 4
  • 24
  • 39