Hi there

I need a bit help with finding a the second smalles number in an array.

Now when I sort the numbers I easly find the biggst and smallest but how can I find the second smallest number when im done sorting the numbers in the array... ?

Code with explanation would be helpfull

Thanks

## 10 Replies - 25652 Views - Last Post: 13 August 2009 - 11:14 AM

##
**Replies To:** Second smallest number in an array?

### #2

## Re: Second smallest number in an array?

Posted 12 August 2009 - 04:52 AM

Quote

Now when I sort the numbers I easly find the biggst and smallest but how can I find the second smallest number when im done sorting the numbers in the array

it will be the second element if there are no duplicates in the array, if there are any duplicates then you will keep going until you find a new element.. as for showing you the code

[rules][/rules]

### #3

## Re: Second smallest number in an array?

Posted 12 August 2009 - 06:16 AM

yeah sure.. here is what I got..

public static void main(String[] args) { double[] values; values = new double[10]; values[0]=64.2; values[1]=15.3; values[2]=91.4; values[3]=8.2; values[4]=48.1; values[5]=23.8; values[6]=72.5; values[7]=13.4; values[8]=121.9; values[9]=323.4; } public static double minMaxDiff(double[] values) throws IllegalArgumentException { if(values.length-1 == 0){ //If the array doesnt consist of 2 or more values it announces a IllegalArgumentException throw new IllegalArgumentException("You have to enter two or more values!"); } Arrays.sort(values); //Sorts values double max = values[values.length-1]; //Finds max value. double min = values[0]; //Finds min value.

### #4

## Re: Second smallest number in an array?

Posted 12 August 2009 - 07:37 PM

If your are allowed to use the Java library sort() method than the numner you want is the second one in the array of the one befor the last depending of your sort order

### #5 Guest_Neumann*

## Re: Second smallest number in an array?

Posted 12 August 2009 - 07:52 PM

One of my code snippets is an example of finding any Kth largest number without fully sorting the array. If you know basics of C you could easily translate it to Java.

### #6

## Re: Second smallest number in an array?

Posted 13 August 2009 - 12:22 AM

pbl, on 12 Aug, 2009 - 06:37 PM, said:

If your are allowed to use the Java library sort() method than the numner you want is the second one in the array of the one befor the last depending of your sort order

I would be careful doing this, just in case you have the same value twice in a single array such as the array { 2, 3, 0, 0, 1 }

If this is sorted it would become { 0, 0, 1, 2, 3 } and hence the value at position 1 would not be the second smallest.

A way i propose would be to find the smallest in the array and essentially create a new array without that value present, then iterate this new array and find the smallest in it, check that it does not equal the smallest from first array and if this returns true - then one could assume it is the second smallest from the first array.

### #7

## Re: Second smallest number in an array?

Posted 13 August 2009 - 01:33 AM

@bbq that's a problem when u have the same value twice... I understand ur idea but how can I code it... because im a beginner.. i mean how to make a new array with all the values but without the smallest one and then search for the smallest again.??

thanks for helping guys

thanks for helping guys

### #8

## Re: Second smallest number in an array?

Posted 13 August 2009 - 03:11 AM

I say you don't make another array and just go through the first one like so, finding the 2nd smallest

the only problem i really see with doing it this way is if some dip decides to enter all the same values, which knowing teachers they will. in which case you may want to do this

But you know your teacher better then I do. I suggest doing the second one though just because you might get some extra mention or something for thinking out of the box.

int smallest = values[0]; for(int i = 1; i < values.length; i++) { if(values[i]!=smallest) { System.out.println("The smallest value is: " + values[i]); break; //or you can store it to a variable and do the printing outside the terminated loop } }

the only problem i really see with doing it this way is if some dip decides to enter all the same values, which knowing teachers they will. in which case you may want to do this

int secondSmallest = -9001; ... int smallest = values[0]; for(int i = 1; i < values.length; i++) { if(values[i]!=smallest) { secondSmallest = values[i]; break; } } if(secondSmallest !=-9001) System.out.println("The second smallest number would be: "+secondSmallest); else //only occurs if there is no smaller value, aka all values are the same System.out.println("Oh, I see what your doing there...well I thought of that. There is no second smallest value");

But you know your teacher better then I do. I suggest doing the second one though just because you might get some extra mention or something for thinking out of the box.

### #9

## Re: Second smallest number in an array?

Posted 13 August 2009 - 05:29 AM

I'm not really following the above if(values[i]!=smallest) {

Since code is on the table, and it seems wrong, here's an approach.

Um, that's it as far as I can tell. No sorting required.

Since code is on the table, and it seems wrong, here's an approach.

double small1, small2; small1 = small2 = values[0]; for(int i = 1; i < values.length; i++) { if(values[i]<small1) { small2 = small1; small1 = values[i]; } }

Um, that's it as far as I can tell. No sorting required.

This post has been edited by **baavgai**: 13 August 2009 - 05:30 AM

### #10

## Re: Second smallest number in an array?

Posted 13 August 2009 - 10:52 AM

My version is assuming the use of a sort, but agreed there is no need for it at all. but what mine is doing is its saying that the smallest number will be the first one. The second smallest is logically the second number, but because of the possibility of it being a repeat it will check if its the same number, if it is then it is not smaller, moves on, until it finds a number that is not the same as the 1st one, then this number must be the second smallest, it will break from the loop and everyone is happy...

### #11

## Re: Second smallest number in an array?

Posted 13 August 2009 - 11:14 AM

then in that case go through the array, checking if there are any repeats, then sort them.

Page 1 of 1