Re: searching through a set of values
Jens.Toerring_at_physik.fu-berlin.de
Date: 05/06/04
- Next message: Alex: "Re: Hello again"
- Previous message: Mabden: "Re: searching through a set of values"
- In reply to: Mabden: "Re: searching through a set of values"
- Next in thread: CBFalconer: "Re: searching through a set of values"
- Reply: CBFalconer: "Re: searching through a set of values"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 6 May 2004 15:31:03 GMT
In comp.programming Mabden <mabden@sbcglobal.net> wrote:
> "Mike" <mikesta@hotmail.com> wrote in message
> news:80214b41.0405060704.2fd456c@posting.google.com...
>> I am kinda new to this so I was looking for some thoughts searching.
>> Just say I have a thousand values in some file. And just say I need to
>> search through that file to see if a given value is inside. I guess
>> there are many ways to do this but I wanted to look at simple
>> efficient ways.
>>
>> One way, is to loop through all values in the file one by one until I
>> find the value. Another way is to sort the values into some sought of
>> alphanumeric order and do a search where you look at the half way
>> point, and depending on the comparison, narrow the search to half the
>> file, and keep using this method to find the value more quickly.
>>
>> What other ways are there? Does it make sense to place these values in
>> a keyed file like a Berkeley DB file and use the tools native to this
>> application to so the search? I am looking for simple but effective
>> ways for getting the search done. Ideas are welcome.
>>
>> PS: I am looking to code the method in C
> Try asking over in comp.lang.c. They love answering homework questions.
No, don't do that. clc isn't for questions about algorithms, so I
removed it from the followup list.
But the question is: do you have to look for a single value in that
file or do you have to do several lookups? In the first case sorting
the values in that array won't help you at all because in order to
sort the data you already have to look at each value at least once
while when you simply scan through all values as they are in the
file you have on average only to look at halve of them. The situation
becomes different when you have to find values repeatedly, then, at
some threshold, sorting the data first will be less expensive. And
for putting the data in a hash the same holds - to create the hash
you also need to look at all the values in the file. And if using a
hash instead of a sorted array is faster for lots of lookups will
also depend on the type of data and the cost of calculating the hash
function versus the cost of doing comparisons.
Regards, Jens
-- \ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de \__________________________ http://www.toerring.de
- Next message: Alex: "Re: Hello again"
- Previous message: Mabden: "Re: searching through a set of values"
- In reply to: Mabden: "Re: searching through a set of values"
- Next in thread: CBFalconer: "Re: searching through a set of values"
- Reply: CBFalconer: "Re: searching through a set of values"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|