How can i get array index to satisfy my requirement fastly?
-
Hi ,i have a questions ,and i don't know how to handle it. I have a double array named array1=new double[100], and each element is function result, like array1[0]=fun(0),array2[1]=fun(1)... each element is positive or negative, and generally speaking, a[0] to a[n] is positive, and a[n+1] to end is negative, so how can i get n ? i need efficient algorithm, like divide-and-conquer method for example... and case 2: if near the end i get wrong result, for example, a[90] is +,how can i get the first negative index n ?
-
Hi ,i have a questions ,and i don't know how to handle it. I have a double array named array1=new double[100], and each element is function result, like array1[0]=fun(0),array2[1]=fun(1)... each element is positive or negative, and generally speaking, a[0] to a[n] is positive, and a[n+1] to end is negative, so how can i get n ? i need efficient algorithm, like divide-and-conquer method for example... and case 2: if near the end i get wrong result, for example, a[90] is +,how can i get the first negative index n ?
In case one, a simple Binary search algorithm - Wikipedia, the free encyclopedia[^] should do it, assumign there is no "bad" data. When you data goes bad, that's different, and it becomes necessary to actually search the whole array - if there is one bad value, then it's likely there are more. But the alternative to both is to to it all while you fill the array - since that is already a time consuming process (compared to a search of the results) adding a little extra checking to find the first negative as you fill the values is trivial, and allows you to cope with the second case as well.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...