LJ Codes
LJ Codes

Reputation: 116

C# Search array within provided index points

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?

Upvotes: 1

Views: 142

Answers (2)

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

Upvotes: 1

Backs
Backs

Reputation: 24903

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

Upvotes: 3

Related Questions