Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# finding the min and max values in an array

Posted on 2003-02-23
Medium Priority
323 Views
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
Question by:surball

LVL 9

Accepted Solution

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

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

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

LVL 9

Expert Comment

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

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

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

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

Expert Comment

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
i regret it.
regards
swargdwar

0

LVL 9

Expert Comment

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

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET â€¦
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done withâ€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
###### Suggested Courses
Course of the Month14 days, 13 hours left to enroll

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

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