10 Replies  13256 Views  Last Post: 13 August 2009  11:14 AM
#1
Second smallest number in an array?
Posted 12 August 2009  04:46 AM
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
Replies To: Second smallest number in an array?
#2
Re: Second smallest number in an array?
Posted 12 August 2009  04:52 AM
Quote
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
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.length1 == 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.length1]; //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
#5 Guest_Neumann*
Re: Second smallest number in an array?
Posted 12 August 2009  07:52 PM
#6
Re: Second smallest number in an array?
Posted 13 August 2009  12:22 AM
pbl, on 12 Aug, 2009  06:37 PM, said:
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
thanks for helping guys
#8
Re: Second smallest number in an array?
Posted 13 August 2009  03:11 AM
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
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
#11
Re: Second smallest number in an array?
Posted 13 August 2009  11:14 AM
