Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Simple but Brain eating

  Asked By: Jason    Date: Jan 02    Category: Java    Views: 676
  

Please tell me how to find minimum of n values

it sounds simple but i caouldnt figure it out.

Make it a recrusive please-

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Corey Jones     Answered On: Jan 02

There are two ways that come to mind.

One is to put the numbers in an array then call
Arrays.sort on this array. The element at offset 0
(the first elemnt in the array will be the smallest.

The other is to loop through all the numbers holding
the current smallest number in a temporary variable.
If the current number is smaller than the temporary
assign its valule to the temporary variable.

The code for both are quite simple  so I will not post
it here but asy so if you need it and I'm sure
somebody on this list(including myself) can give you
the code.

 
Answer #2    Answered By: Troy Kelley     Answered On: Jan 02

use the Math class for finding out the min,
pass a List of numbers to the userdefined function,
recursively call the function.

 
Answer #3    Answered By: Gorkem Yilmaz     Answered On: Jan 02

int minIndex(int v[], int first, int last){
if(first==last){return first;}
int tempA = min(v,first,(first+last)/2);
int tempB = min(v,(first+last)/2,last);
if(tempA<tempB) return tempA;
else return tempB;
}

 
Answer #4    Answered By: May Hansen     Answered On: Jan 02

need to compare the ints in the array, not the indexes.
Should be:
int minIndex(int v[], int first, int last){
if(first==last){return first;}
int tempA = min(v,first,(first+last)/2);
int tempB = min(v,(first+last)/2,last);
if(v[tempA]<v[tempB]) return tempA;
else return tempB;

 
Didn't find what you were looking for? Find more on Simple but Brain eating Or get search suggestion and latest updates.




Tagged: