• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Another dumb java question!

demenion

Golden Member
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! 😛)



 
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
 
sorting is going to be your best option, but you could also get into som nasty loop structures that would do it too.
 
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 ]);
}
 
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 ];
 
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.
 
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.
 
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.
 
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.
 
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.
 
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?
 
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.
 
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.
 
Back
Top