finding the min and max values in an array

Could anybody give me an efficient algorithm for finding the min and max values in an unsorted array. Its not just one array that i have to process, but it may be about 20000 arrays. There is 1 min and the 1 max is for all the arrays together.
surballAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yongsingCommented:
Since the array is unsorted, you would have to go through every item in the array:

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < yourArray.length; i++) {
  min = yourArray[i] < min ? yourArray[i] : min;
  max = yourArray[i] > max ? yourArray{i] : max;
}
System.out.println("Min = " + min);
System.out.println("Max = " + max);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
functionpointerCommented:
yongsing is right. you have to check every value. but if you wrap a controller type class around all your arrays could limit the amount of searching you have to do. That way you would only have to check the min/max on addition, removal(if the array contained a min or max), or alteration of the data in the underlying arrays ( which you could control in the accessor as well ).
0
swargdwarCommented:
youngsing's code is wrong!!!!!!!!!!!!!!!!!

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < yourArray.length; i++) {
 min = yourArray[i] < min ? yourArray[i] : min;
 max = yourArray[i] > max ? yourArray{i] : max;
}
System.out.println("Min = " + min);
System.out.println("Max = " + max);

/********************************************/
NO INTEGER WILL BE LESS THAN min(Integer.MIN_VALUE).
HE SHOULD HAVE INITIALIZED IT AS

int min = yourArray[0] ;
int max = yourArray[0] ;

/*************************************************/


0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

yongsingCommented:
>> NO INTEGER WILL BE LESS THAN min(Integer.MIN_VALUE).

That's true. I never claim that any integer will be less than Integer.MIN_VALUE. My code still works.
0
swargdwarCommented:
suppose his array contains

min value of -1000
and max value of +1000

ur code will work but it will show
max=Integer.MAX_VALUE;
min=Integer.MIN_VALUE;

not
max=1000
min=-1000

because
1000!>Integer.MAX_VALUE  //max = yourArray[i] > max ? yourArray{i] : max;

-1000!<Integer.MIN_VALUE //min = yourArray[i] < min ? yourArray[i] : min;

0
yongsingCommented:
Well, why don't you try this:

public class TestMinMax {

  public static void main(String[] args){
    int[] yourArray = {0, -1, 345, -45, 1000, -666, -1000, 234};
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < yourArray.length; i++) {
      min = yourArray[i] < min ? yourArray[i] : min;
      max = yourArray[i] > max ? yourArray[i] : max;
    }
    System.out.println("Min = " + min);
    System.out.println("Max = " + max);
  }
}

It prints:

Min = -1000
Max = 1000

Perhaps you're convinced now.
0
surballAuthor Commented:
The answer that yongsing has given (and i accepted it as the answer) works fine
0
swargdwarCommented:
hi youngsing
i am sorry man!
ur code works because

int min = Integer.MAX_VALUE;//here min is equal to MAX
int max = Integer.MIN_VALUE;//here max is equal to MIN

i did not notice this swapping nor did u point this out in
ur comments.
i regret it.
regards
swargdwar

0
yongsingCommented:
No problem, swargdwar. :) In fact, initializing the min and max variables to the first element of the array might be a better idea, because it's not that confusing.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.