Another dumb java question!

demenion

Golden Member
Nov 11, 1999
1,552
0
0
If I have a group of doubles, say each assigned to something like:
a, b, c, d, and e

if each variable's value is inputted by the user, how could i find which one is the highest and lowest value?

i assume it would require a nested loop, but i'm still a beginner and don't really know how to start! ;p

sorry! i feel helpless

(eh maybe another question later! :p)



 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
I don't know Java, so I'll speak generally. Put them in a list (or array, or whatever), sort the list, and then use the last item.

In Python:

>>> f = [1,7,93,2,78,9]
>>> f.sort()
>>> biggest = f[-1]
>>> print biggest
93
 

jonmullen

Platinum Member
Jun 17, 2002
2,517
0
0
sorting is going to be your best option, but you could also get into som nasty loop structures that would do it too.
 

ys

Senior member
Oct 10, 1999
757
0
0
assuming you only have a, b, c, d, and e.

double[] array = new double[] {a,b,c,d,e};

double max = 0;
double min = a;
for (int i = 0; i < array.length; i++)
{
max = Math.max(max, array[ i ]);
min = Math.min(min, array[ i ]);
}
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Oh crap I totally forgot about min and max.

But why the loop? In Python you can just do:

>>> max([1,7,93,2,78,9])
93
 

ys

Senior member
Oct 10, 1999
757
0
0
I am not aware if there are Java package which would do that, but that "Python max" sure is convenient!

Maybe we can do the sorting style in Java too:

double[] array = new double[]{a,b,c,d.e};
double min = 0, max = 0;
Arrays.sort(array);
min = array[ 0 ];
max = array[ array.length - 1 ];
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
max = array[0]
for(int ii == 1; i < array.length; i++){
if(array[ii] > max){
max = array[ii]
}

Sorting is NOT your best option by far. Show me a sorting algorithm with a constant growth rate.

There may be a builtin 'max' function in java that does the same thing I did above, but it's not something I know off the top of my head.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: notfred
max = array[0]
for(int ii == 1; i < array.length; i++){
if(array[ii] > max){
max = array[ii]
}

Sorting is NOT your best option by far. Show me a sorting algorithm with a constant growth rate.

But it's only 5 items. If it was 100,000 items, then speed is a concern -- with 5, it definitely isn't.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: BingBongWongFooey
Originally posted by: notfred
max = array[0]
for(int ii == 1; i < array.length; i++){
if(array[ii] > max){
max = array[ii]
}

Sorting is NOT your best option by far. Show me a sorting algorithm with a constant growth rate.

But it's only 5 items. If it was 100,000 items, then speed is a concern -- with 5, it definitely isn't.

Even still, write a sorting algorithm simpler than my code.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: notfred
Originally posted by: BingBongWongFooey
Originally posted by: notfred
max = array[0]
for(int ii == 1; i < array.length; i++){
if(array[ii] > max){
max = array[ii]
}

Sorting is NOT your best option by far. Show me a sorting algorithm with a constant growth rate.

But it's only 5 items. If it was 100,000 items, then speed is a concern -- with 5, it definitely isn't.

Even still, write a sorting algorithm simpler than my code.

Why would I write a sorting algorithm? It's built in.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: BingBongWongFooey
Originally posted by: notfred
Originally posted by: BingBongWongFooey
Originally posted by: notfred
max = array[0]
for(int ii == 1; i < array.length; i++){
if(array[ii] > max){
max = array[ii]
}

Sorting is NOT your best option by far. Show me a sorting algorithm with a constant growth rate.

But it's only 5 items. If it was 100,000 items, then speed is a concern -- with 5, it definitely isn't.

Even still, write a sorting algorithm simpler than my code.

Why would I write a sorting algorithm? It's built in.

Why would you write this program in the first place? Do you really need a peice of softare to pick the largest of five numbers for you? This isn't a practical application he's building.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Hmm...notfred's way is definitely the easiest.

On a related question...for larger sets of data...would the fastest method be to say read the values into a binary tree and then output the right-most value for max and the left-most for min? Or would it be easier to say, sort (quicksort?) the arrary and print the last (or first) value?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: notfred
Why would you write this program in the first place? Do you really need a peice of softare to pick the largest of five numbers for you? This isn't a practical application he's building.

Dunno, the purpose was never mentioned.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: eLiu
Hmm...notfred's way is definitely the easiest.

On a related question...for larger sets of data...would the fastest method be to say read the values into a binary tree and then output the right-most value for max and the left-most for min? Or would it be easier to say, sort (quicksort?) the arrary and print the last (or first) value?

It would be fastest to just go through and pick the maximum like I did in my earlier post.

If the values were already sorted in a binary search tree, then picking the max value out of the tree would be faster, however if you had to create the tree, then it wouldn't be faster. Quicksort is slower than running through the list once like I did above.

Big-O values:

Searching binary tree: log N
Building binary tree: N
Building and searching binary tree: N log N
notfred's one-pass method: N
Quicksort: N log N

BTW, log N is faster than N which is faster than N log N.