# Search Algorithm C#

Experts,

Platform : C# . NET VS 2008 3.5

I`m trying to implement a search algorithm.

I have list of doubles and a list of objects of a certain type. I`m trying to find if the values in the list of doubles match the objects in and around a precision value, if they do, I want the Index of the object.

Now if the value is not present I want to return a null value. With the code I have now, I always return a value - that`s where the issue is.

``````//TimeVals is the List of Doubles. Sorted/
//AObjList is the List of Objects of a certain class
//FmZero = 0.0001 This is the precision value

for (i = 0; i < TimeVals.Count - 1; i++)
{
Ind = GetInd(TimeVals[i], AObjList, ref Ind);
Console.WriteLine(Ind);
}

private int? GetInd(double Time, List<Obj> LObj, ref int? Ind)
{
int Tail = LObj.Count - 1;

if (Time < LObj[(int)Ind].Time)
{
while ((Ind != Head) && Time < (LObj[(int)Ind].Time - GP.FmZero))
{
Ind = Ind - 1;
}
}
else
{
while ((Ind != Tail) && Time >= (LTrj[(int)Ind].Time - GP.FmZero))
{
Ind = Ind + 1;
}
}

return Ind;
}
``````
###### Who is Participating?

Commented:
Structurally, line 22 is fine. Your problem, however, is that you never update the value of Found anywhere in that function. It will always be false. You need to change your logic to update the value of Found.
0

Commented:
You've denoted Ind to be a reference parameter by your use of the ref keyword. ref expects that its corresponding parameter be initialized prior to passing it to the function. How are you initializing the Ind variable prior to line 7?
0

Author Commented:

I get what you are trying to say, there is no point in returning a null if I`m using the ref keyword. It would need to be initialized.

Cause initially I was thinking I could return a null if the value was not found, and then do my calculations -

``````for (i = 0; i < TimeVals.Count - 1; i++)
{
Ind = GetInd(TimeVals[i], AObjList, ref Ind);
if(Ind!=null){
//Calculations here
Console.WriteLine(Ind);
}
}
``````
0

PresidentCommented:
kaufmed has the problem pinpointed.

It is never a good ideas anyway to pass an int by reference, since it involves boxing, a memory manipulation that is not good for performance.

And since you are returning Ind at the end of the method, you do not need the ref anyway. Simply remove the ref.
0

Author Commented:
@JamesBurger - Makes sense.  How do I return a null if the value is not found? Right now Ind always has a value.
0

Commented:
You declared the return value to be of type int?, or Nullable<int>. You can simply return null.

i.e.

``````return null;
``````
0

Author Commented:
Yes, but where would I use that in the code. The way GetInd function is written it always returns a value.
For example where would I set the bool Found? I`m guessing I`ll have to rewrite the function is a different way.
``````private int? GetTrj(double Time, List<Trajectory> LTrj, int? Ind)
{
int Tail = LTrj.Count - 1;
bool Found = false;

if (Time < LTrj[(int)Ind].Time)
{
while ((Ind != Head) && Time < (LTrj[(int)Ind].Time - GP.FmZero))
{
Ind = Ind - 1;
}
}
else
{
while ((Ind != Tail) && Time >= (LTrj[(int)Ind].Time - GP.FmZero))
{
Ind = Ind + 1;
}
}

return (Found == false ? null : Ind);
}
``````
0

PresidentCommented:
Nullable are useful, specially with databases. If they suit you, go for it.

But a regular int is easier to work with and takes up less ressources, so when it is possible, there is an alternative: return a set value that could not be returned if something was found, and treat this value as a null.

With integers, I often use int.MinValue as "my" null. I usulally do not expect -2147483648 as a value for one of my integers, so it is easy for me to interpret that value as being null.
0

Author Commented:
@Kaufmed - Thats what I need help with, I want to know where I can set the Found value. A sample code would be greatly appreciated.
0

Author Commented:
``````int i = 0;
int? Ind = 0;
int? SInd = 0;                    //Begin search at this index
double APos = 0;
int EInd = 0;                     //Exist Index. Increment only if the the given Time value was found.

for (i = 0; i < TimeCnt - 1; i++)
{
Ind = GetTrj(TimeVals[i], ATrj, SInd);

if (Ind != null)
{
APos = ATrj[(int)Ind].Pos;
TableGV[a, i].Value = APos;
SInd = Ind;
EInd++;
}
else
{
Ind = SInd;
TableGV[a, i].Value = null;
}
}

private int? GetTrj(double Time, List<Trajectory> LTrj, int? SInd)
{
int Tail = LTrj.Count - 1;
bool Found = false;
int Ind = 0;

if (Time < LTrj[(int)SInd].Time)
{
for (Ind = (int)SInd; Ind > Head; Ind--)
{
if ((Time <= (LTrj[Ind].Time + GP.FmZero)) && (Time >= (LTrj[Ind].Time - GP.FmZero)))
{
Found = true;
break;
}
}
}
else
{
for (Ind = (int)SInd; Ind < Tail; Ind++)
{
if ((Time <= (LTrj[Ind].Time + GP.FmZero)) && (Time >= (LTrj[Ind].Time - GP.FmZero)))
{
Found = true;
break;
}
}
}

return (Found == false ? null : (int?)Ind);
}
``````
0

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.