I have this code that does a linear search within two files, but it is too slow......
I want to change it into binarysearch so it will perform faster, but the problem is I don't know how can I do that, I mean, if I change it to binarysearch the structure itself will have to be changed. I have the following questions that I can't solve so the binarysearch can be done:
1. Cut the sentences into words.
2. Compare first word of the sentences and see if matches, if match take second and so on till a phrase in my standard file is formed. Otherwise, discard word.
e.g. Lets say I have a sentences "This world is full of beauty!" and I have in my sorted array the phrase "full of beauty", I would like to take each word of the sentence to and compare it with my array like the following:
1. Take the word "This", since it doesn't match delete it
2. Take the word "world", since it doesn't match delete it.
3. and so on until "full", since "full" is part of my phrase "full of beauty" conserve it and take the next and compare it, and so on till I get the full phrase "full of beauty" then return that the phrase was found.
Hope someone can give me some hint, I really need to get this thing to work, and want to get it work myself but I really can't do. Thanks in advance to all.
Code:
for(unsigned i=0;i<Standard.size();i++)
{
for(unsigned j=0;j<MyWords.size();j++) //each MyWords compare to the Standard
//and see if it is in the standard vector
{
string::size_type FoundAt=MyWords[i].find(Standard[j]);
while( string::npos != FoundAt )
{
MyWords[i].replace( FoundAt, Standard[j].length(),Standard[j]+"/");
FoundAt = MyWords[i].find( Standard[j], FoundAt + Standard[j].length() );
}
}
}
/***********************************Create output file****************************************/
for(unsigned j=0;j<MyWords.size();j++) // for each word in MyWords vector
{
WriteWord<<MyWords[j]<<endl; // write it to the destination file
}
WriteWord.flush(); // flush the memory to ensure all is written to the file
WriteWord.close(); // close the write file handls
return 0;
}
1. Cut the sentences into words.
2. Compare first word of the sentences and see if matches, if match take second and so on till a phrase in my standard file is formed. Otherwise, discard word.
e.g. Lets say I have a sentences "This world is full of beauty!" and I have in my sorted array the phrase "full of beauty", I would like to take each word of the sentence to and compare it with my array like the following:
1. Take the word "This", since it doesn't match delete it
2. Take the word "world", since it doesn't match delete it.
3. and so on until "full", since "full" is part of my phrase "full of beauty" conserve it and take the next and compare it, and so on till I get the full phrase "full of beauty" then return that the phrase was found.
Hope someone can give me some hint, I really need to get this thing to work, and want to get it work myself but I really can't do. Thanks in advance to all.
Comment