Efficent method to find index of a value in a array
-
Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.
double[] a = new double[]{1,2,3,4}
I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)
-
Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.
double[] a = new double[]{1,2,3,4}
I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)
TheGermoz wrote:
is better using LINQ
No. Probably not anyway, but I'm unclear what you are trying to do.
-
Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.
double[] a = new double[]{1,2,3,4}
I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)
100 is a stupid failure value. What if you have a 100 element array? -1 is traditional for 'not found' (see Array.IndexOf and the like). I think your expected output is wrong ... if F[4] is 3 then F[2] must be 1. Does
int F(double[] arr, int value){
return -1 + Array.FindIndex(arr, d => d >= value);
}... do what you want?
-
Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.
double[] a = new double[]{1,2,3,4}
I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)
If the values are monotonically increasing, Array.BinarySearch[^] will probably be the most efficient way to find a particular value.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.
double[] a = new double[]{1,2,3,4}
I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)
-
Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.
double[] a = new double[]{1,2,3,4}
I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)
TheGermoz wrote:
It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)
The difference in performance would only just be better for LINQ. If your function is not many to one, you could directly use a key value pair collection.
WP7.5 Apps - XKCD | Calvin | SMBC | Sound Meter | Speed Dial
-
100 is a stupid failure value. What if you have a 100 element array? -1 is traditional for 'not found' (see Array.IndexOf and the like). I think your expected output is wrong ... if F[4] is 3 then F[2] must be 1. Does
int F(double[] arr, int value){
return -1 + Array.FindIndex(arr, d => d >= value);
}... do what you want?
yes sorry my error F[2] must be 1. Yes is solve it. The only error is for F[1] should be 0 and not -1. Is it more efficient Linq or BinarySearch?