Solved

Search Algorithm C#

Posted on 2011-09-13
12
417 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 30
What Does This C# Code Block Do? 5 55
JSON Deserialize issue 6 26
call a controller action method from Global.asax in asp.net 1 20
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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

839 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