# sorting min and max values in an array

Page 1 of 1

## 7 Replies - 12875 Views - Last Post: 18 November 2010 - 10:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=200907&amp;s=bab9f1af05524f3c3190243f6e88183f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 geraldined

Reputation: 0
• Posts: 8
• Joined: 13-November 10

# sorting min and max values in an array

Posted 18 November 2010 - 05:17 PM

I don't understand what this portion of my code means can anybody tell me the parts to it???

```
int min = Integer.MAX_VALUE, minIndex = 0;
int max = Integer.MIN_VALUE, maxIndex = 0;

```

and this is the entire code if anyone needs it to help tell me what it is...
```
public class MinMaxReverse {

public static void main(String[] args) {
int[] arr = {1, 2, -3000, 4, 5, -100, 3, 50000, 700, 2, 4, 6, 8000};
int[] rev_arr = new int [arr.length];

int min = Integer.MAX_VALUE, minIndex = 0;
int max = Integer.MIN_VALUE, maxIndex = 0;

for (int i = 0; i < arr.length; i++) {
if (arr[i] < min)
min=arr[i];
if (arr[i] < max)
max=arr[i];
/* Fill in your code here to find min/max
* There's only one loop needed
*/
}

System.out.println("Min number is " + min + " found at index " + minIndex);
System.out.println("Max number is " + max + " found at index " + maxIndex);

for (int i = 0; i < arr.length; i++) {
if (rev_arr[i] > min)
max=i;
if (rev_arr[i] < max)
min=i;
/* Fill in your code here
* To reverse the arr into rev_arr
*/
}

// Printing out the results
System.out.println("\nPrinting out both array contents and they should be the same");
for (int i = arr.length; i > 0; i--) {
System.out.print("Arr[" + (arr.length-i) + "]=" + arr[arr.length-i]);
System.out.println("\tsame as \tRev_Arr[" + (i-1) + "]=" + rev_arr[i-1]);
}

}

}

```

I am aware that the places where I'm supposed to fill in the code are wrong. I'm hoping that by understanding what the part I had in question means will help me figure out what code to put in.

Is This A Good Question/Topic? 0

## Replies To: sorting min and max values in an array

### #2 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: sorting min and max values in an array

Posted 18 November 2010 - 05:23 PM

Quote

I don't understand what this portion of my code means can anybody tell me the parts to it???

hmmm... your code, and you don't know what it means? weird...
Anyways...
from Java API:
Integer.MAX_VALUE:
A constant holding the maximum value an int can have, 2^31 -1.
Integer.MIN_VALUE:
A constant holding the minimum value an int can have, -2^31.

```int min = Integer.MAX_VALUE, minIndex = 0;
int max = Integer.MIN_VALUE, maxIndex = 0;
```

you declare two int variables, min and max, and assign them the minimu and maximum values an int variable can hold.

### #3 geraldined

Reputation: 0
• Posts: 8
• Joined: 13-November 10

## Re: sorting min and max values in an array

Posted 18 November 2010 - 05:29 PM

Well technically it's for a lab I have and our task is to fill in the rest of the code. I should have put "code I'm working with" Sorry.
so what does the minIndex=0 and maxIndex=0 part mean?

### #4 Travis1012

Reputation: 36
• Posts: 226
• Joined: 05-August 09

## Re: sorting min and max values in an array

Posted 18 November 2010 - 05:31 PM

geraldined, on 19 November 2010 - 09:17 AM, said:

I don't understand what this portion of my code means can anybody tell me the parts to it???

```
int min = Integer.MAX_VALUE, minIndex = 0;
int max = Integer.MIN_VALUE, maxIndex = 0;

```

ok well int min = Integer.MAX_VALUE declares the Integer variable min to be the maximum value an Integer can have which is 2147483647. Now when you continue the same line with , minIndex = 0; it means that you are also declaring minIndex as an Integer but of value 0.

The part of the next line int max = Integer.MIN_VALUE declares the Integer variable max to be the minimum value an Integer can have which is -2147483648. Now when you continue the same line with , maxIndex = 0; it means that you are also declaring maxIndex as an Integer but of value 0.

This post has been edited by Travis1012: 18 November 2010 - 06:08 PM

### #5 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: sorting min and max values in an array

Posted 18 November 2010 - 05:33 PM

minIndex and maxIndex are more logic variables declared by "you" to store the index in the array of the min value (stored in minIndex) and the max value (stored in maxIndex).
They are initialized to 0 as default (0 is the value the compiler initialize global variables to), or perhaps because that is the first index in arrays. (Both options are logic, and rational).

### #6 geraldined

Reputation: 0
• Posts: 8
• Joined: 13-November 10

## Re: sorting min and max values in an array

Posted 18 November 2010 - 05:36 PM

Oh okay thank you!!

### #7 geraldined

Reputation: 0
• Posts: 8
• Joined: 13-November 10

## Re: sorting min and max values in an array

Posted 18 November 2010 - 10:15 PM

Could someone tell me why the min and max are set to Integer.MAX_VALUE and Integer.MIN_VALUE??? What's the point?

### #8 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

## Re: sorting min and max values in an array

Posted 18 November 2010 - 10:24 PM

geraldined, on 18 November 2010 - 09:15 PM, said:

Could someone tell me why the min and max are set to Integer.MAX_VALUE and Integer.MIN_VALUE??? What's the point?

Well, unless you know the range of your values, how will you ever know what to set the initial min/max at? 0? What if there's a 1 million min? Zero certainly isnt the min... For practicality and reusability sake, you want to set aside any hardcoding done...setting the values as you said accomplishes that:

The min is never going to be higher than the max integer value and the max is never going to be lower than the min value possible, right?
Alright, so the max integer value is 2.147million something. Now, all this does is allows you to start at a point which you know has to be the extremest case.
So for example...for max:
You see a 10.
Assign 10.
New Max is 10.
Keep repeating...
Guarantees that you will get it right every time versus