Second smallest number in an array?

How to find second smallest number in an array

Page 1 of 1

10 Replies - 12774 Views - Last Post: 13 August 2009 - 11:14 AM Rate Topic: -----

#1 TKOH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 09

Second smallest number in an array?

Post icon  Posted 12 August 2009 - 04:46 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Second smallest number in an array?

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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]
Was This Post Helpful? 0
  • +
  • -

#3 TKOH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 09

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.
		

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Neumann*


Reputation:

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.
Was This Post Helpful? 0

#6 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Second smallest number in an array?

Posted 13 August 2009 - 12:22 AM

View Postpbl, 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.
Was This Post Helpful? 0
  • +
  • -

#7 TKOH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 09

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
Was This Post Helpful? 0
  • +
  • -

#8 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

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

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.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

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.

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

Was This Post Helpful? 0
  • +
  • -

#10 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

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...
Was This Post Helpful? 0
  • +
  • -

#11 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1