1

I'm not sure how best to phrase this. I have a text file of almost 80,000 words which I have converted across to a string array.

Basically I want a method where I pass it a word and it checks if it's in the word string array. To save it searching 80,000 each time I have indexed the locations where the words beginning with each letter start and end in a two dimensional array. So wordIndex[0,0] = 0 when the 'A' words start and wordIndex[1,0] = 4407 is where they end. Then wordIndex[0,1] = 4408 which is where the words beginning with 'B' start etc.

What I would like to know is how can I present this range to a method to have it search for a value. I know I can give an index and length but is this the only way? Can I say look for x within range y and z?

2
  • I would create a method you call to search for the word, this would find the indexes into that 2d-array and grab the indexes to the word array out of it. Then I would pass the word + the indexes to a method that would do a binary search for the word. If you pass inn 'STACK' as a word, I would grab the index for 'ST' from your array as well as for 'SU' (S + next letter after T), then search in that range using binary search. Commented Aug 8, 2015 at 10:54
  • Having said that there are already existing data structures in .NET like a Dictionary or a HashSet that can be used to good effect for this kind of problem. Commented Aug 8, 2015 at 10:54

2 Answers 2

3

Look at Trie set. It can help you to store many words using few memory and quick search. Here is good implementation.

Sign up to request clarification or add additional context in comments.

3 Comments

I think I might be getting a bit ahead of myself if I looked into these. It's definitely something I will consider in the future though.
@LJSpring be brave :) it' very good data structure and has "easy" implementation. if you need some help - just ask
Thanks Backs :) Just don't want to move quicker than the material provided by the lecturer at the moment. Might surprise him with this at some point though!
1

Basically you could use a for loop to search just a part of the array:

string word = "apple";
int start = 0;
int end = 4407;
bool found = false;

for (int i = start; i <= end ; i++)
{
    if (arrayOfWords[i] == word)
    {
        found = true;
        break;
    }
}

But since the description of your index implies that your array is already sorted a better way might be to go with Array.BinarySearch<T>.

1 Comment

Aha, yes of course, I was too busy thinking there was a shortcut and didn't stop to think I could just loop it. This might be the quick and dirty solution for me. Was just hoping for some elegant method. Many thanks. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.