java - is this the most efficient way? -


i'm looking greatest difference between 2 doubles in list, have done way in nlogn time, there way in linear time? thanks!

public static double nlogn(double[] ar){     arrays.sort(ar);     double max=ar[ar.length-1];     double min=ar[0];     double difference=max-min;     return difference; } 

how not sorting array first, traversing it, collecting minimum , maximum value?

public static double linear(double[] ar) {     double max = double.negative_infinity;     double min = double.positive_infinity;      for(double elem: ar){         if(min > elem) {min = elem;}         if(max < elem) {max = elem;}     }      return math.abs(max-min);  } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -