finding min and max of a double[]

J

jimgardener

i tried diff ways to find min and max of a double[] .one using for
loops and the other with Arrays.sort

public void findmaxdemo1(){
double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
long start=System.nanoTime();
double max=testarray[testarray.length-1];
double min=testarray[0];
for(int i=0;i<testarray.length;i++){
max=max>testarray?max:testarray;
min=min<testarray?min:testarray;
}
long end=System.nanoTime();
debug("min:"+min);
debug("max:"+max);
debug("by for loops took:"+(end-start));

}

public void findmaxdemo2(){
double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
long start=System.nanoTime();
Arrays.sort(testarray);
double max=testarray[testarray.length-1];
double min=testarray[0];
long end=System.nanoTime();
debug("min:"+min);
debug("max:"+max);
debug("by Array took:"+(end-start));
}

public static void debug(String msg){
System.out.println(msg);
}

i find that the method using Arrays.sort takes more time than the
first one..i would like to know if the first one can be made faster?
I am not familiar with alg.analysis methods.Can someone advise as to
how i can find the max and min more efficiently

thanks
jim
 
V

voorth

i tried diff ways to find min and max of a  double[] .one using for
loops and the other with Arrays.sort

public void findmaxdemo1(){
                double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
                long start=System.nanoTime();
                double max=testarray[testarray.length-1];
                double min=testarray[0];
                for(int i=0;i<testarray.length;i++){
                        max=max>testarray?max:testarray;
                        min=min<testarray?min:testarray;
                }
                long end=System.nanoTime();
                debug("min:"+min);
                debug("max:"+max);
                debug("by for loops took:"+(end-start));

        }

public void findmaxdemo2(){
                double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
                long start=System.nanoTime();
                Arrays.sort(testarray);
                double max=testarray[testarray.length-1];
                double min=testarray[0];
                long end=System.nanoTime();
                debug("min:"+min);
                debug("max:"+max);
                debug("by Array took:"+(end-start));
        }

public static void debug(String msg){
                System.out.println(msg);
        }

i find that the method using Arrays.sort takes more time than the
first one..i would like to know if the first one can be made faster?
I am not familiar with alg.analysis methods.Can someone advise as to
how i can find the max and min more efficiently


Did you try this with large arrays? Six elements is really not long
enough to get reliable metrics. Apart from that, I'm not surprised
that the first method is faster - remember that Arrays.sort() also
moves all intermediate values.

One possible improvement is to use explicit "if" statements instead of
the "?" operator. That will save you some assignments to max:

if(testarray > max) max = testarray;
if(testarray < min) min = testarray;

but I doubt if it's worth the trouble.

--Henk
 
G

GArlington

i tried diff ways to find min and max of a double[] .one using for
loops and the other with Arrays.sort
public void findmaxdemo1(){
double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
long start=System.nanoTime();
double max=testarray[testarray.length-1];
double min=testarray[0];
for(int i=0;i<testarray.length;i++){
max=max>testarray?max:testarray;
min=min<testarray?min:testarray;
}
long end=System.nanoTime();
debug("min:"+min);
debug("max:"+max);
debug("by for loops took:"+(end-start));

public void findmaxdemo2(){
double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
long start=System.nanoTime();
Arrays.sort(testarray);
double max=testarray[testarray.length-1];
double min=testarray[0];
long end=System.nanoTime();
debug("min:"+min);
debug("max:"+max);
debug("by Array took:"+(end-start));
}

public static void debug(String msg){
System.out.println(msg);
}
i find that the method using Arrays.sort takes more time than the
first one..i would like to know if the first one can be made faster?
I am not familiar with alg.analysis methods.Can someone advise as to
how i can find the max and min more efficiently

Did you try this with large arrays? Six elements is really not long
enough to get reliable metrics. Apart from that, I'm not surprised
that the first method is faster - remember that Arrays.sort() also
moves all intermediate values.

One possible improvement is to use explicit "if" statements instead of
the "?" operator. That will save you some assignments to max:

if(testarray > max) max = testarray;
if(testarray < min) min = testarray;

but I doubt if it's worth the trouble.

--Henk


And you can put second if inside the 1st if's else part. Again both
improvements are NOT going to save you much...
 
T

Tom McGlynn

i tried diff ways to find min and max of a double[] .one using for
loops and the other with Arrays.sort
public void findmaxdemo1(){
double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
long start=System.nanoTime();
double max=testarray[testarray.length-1];
double min=testarray[0];
for(int i=0;i<testarray.length;i++){
max=max>testarray?max:testarray;
min=min<testarray?min:testarray;
}
long end=System.nanoTime();
debug("min:"+min);
debug("max:"+max);
debug("by for loops took:"+(end-start));
}
public void findmaxdemo2(){
double[] testarray=new double[]{-1.2,4.3,7,0,2.1,0.0,2.5};
long start=System.nanoTime();
Arrays.sort(testarray);
double max=testarray[testarray.length-1];
double min=testarray[0];
long end=System.nanoTime();
debug("min:"+min);
debug("max:"+max);
debug("by Array took:"+(end-start));
}
public static void debug(String msg){
System.out.println(msg);
}
i find that the method using Arrays.sort takes more time than the
first one..i would like to know if the first one can be made faster?
I am not familiar with alg.analysis methods.Can someone advise as to
how i can find the max and min more efficiently

Did you try this with large arrays? Six elements is really not long
enough to get reliable metrics. Apart from that, I'm not surprised
that the first method is faster - remember that Arrays.sort() also
moves all intermediate values.
One possible improvement is to use explicit "if" statements instead of
the "?" operator. That will save you some assignments to max:
if(testarray > max) max = testarray;
if(testarray < min) min = testarray;

but I doubt if it's worth the trouble.

And you can put second if inside the 1st if's else part. Again both
improvements are NOT going to save you much...



I don't think that's right given the way the OP initialized the loop.
Had the initialization been:

min = max = testarray[0];

then you'd be correct, but with

min = testarray[0]; max = testarray[testarray.length-1];

the initial conditions of the loop may have min>max. I think the OP
thought that catering to the case where the original array was ordered
would be helpful.


A fairly robust algorithm that incorporates your suggestion and should
be about as fast as any is

min=max=Double.NaN;
if (testarray.length > 0) {

min=max=testarray[0];

for (int i=1; i<testarray.length; i += 1) {
double test = testarray;
if (test < min) {
min = test;
} else if (test > max) {
max = test;
}
}
}


One may also need to decide how to handle NaNs when they're
considering a general algorithm.

Tom McGlynn
 
U

Uwe Schmitt

i tried diff ways to find min and max of a  double[] .one using for
loops and the other with Arrays.sort



i find that the method using Arrays.sort takes more time than the
first one..i would like to know if the first one can be made faster?

This is because sorting needs time O(log n), an iterating over
an array is O(n). As you have to "touch" each element for finding
max and min you can not find an algorithm which is faster than O(n).

The following solution should be a little bit faster than your
solution:

double max=testarray[0];
double min=testarray[0];

for(int i=1; i<testarray.length; i++){

if (testarray>max)
max = testarray;

else if (testarray<min)
min = testarray;
}

Greetings, Uwe
 
U

Uwe Schmitt

i tried diff ways to find min and max of a  double[] .one using for
loops and the other with Arrays.sort
i find that the method using Arrays.sort takes more time than the
first one..i would like to know if the first one can be made faster?

This is because sorting needs time O(log n), an iterating over
^^^^^^^^
Wanted to say "O(n log n)"

Greetings, Uwe
 
R

Roedy Green

max=max>testarray?max:testarray;


Math.max will do this for you.

e.g. max = Math.math( max, testArray[ i ] );

If speed is important, code it like this

if (testArray[i ] > max ){ max = testArray;}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top