Solved

Search Algorithm C#

Posted on 2011-09-13
12
409 Views
Last Modified: 2013-12-17
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 Head = 0;
            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;
        }

Open in new window

0
Comment
Question by:San24
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 334 total points
ID: 36530022
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 Comment

by:San24
ID: 36530084
@Kaufmed - I start with Ind=0;

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);
}
                        }

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 166 total points
ID: 36530091
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 Comment

by:San24
ID: 36530154
@JamesBurger - Makes sense.  How do I return a null if the value is not found? Right now Ind always has a value.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36530188
You declared the return value to be of type int?, or Nullable<int>. You can simply return null.

i.e.

return null;

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:San24
ID: 36530258
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 Head = 0;
            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);
        }

Open in new window

0
 
LVL 40
ID: 36530284
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
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 334 total points
ID: 36530312
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
 

Author Comment

by:San24
ID: 36530327
@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 Comment

by:San24
ID: 36530911
I have it working now. What do you guys think about this.
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 Head = 0;
            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);
        }

Open in new window

0
 
LVL 100

Expert Comment

by:mlmcc
ID: 36998427
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now