Solved

Search Algorithm C#

Posted on 2011-09-13
12
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 75

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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

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 75

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
 

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 75

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 101

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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

734 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