?
Solved

finding the min and max values in an array

Posted on 2003-02-23
9
Medium Priority
?
319 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:surball
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 9

Accepted Solution

by:
yongsing earned 200 total points
ID: 8002582
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8004042
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
 

Expert Comment

by:swargdwar
ID: 8007260
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:yongsing
ID: 8007361
>> 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
 

Expert Comment

by:swargdwar
ID: 8007497
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
 
LVL 9

Expert Comment

by:yongsing
ID: 8013187
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
 

Author Comment

by:surball
ID: 8013213
The answer that yongsing has given (and i accepted it as the answer) works fine
0
 

Expert Comment

by:swargdwar
ID: 8014213
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
 
LVL 9

Expert Comment

by:yongsing
ID: 8014279
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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question