I was thinking about search functions, and how they work, and while I imagine this has been done before, I want to know if it's a good idea:
Code:search(char target[], char strtofind[], int stfsiz) { int i = 0; int dectr; while((i+stfsiz) <= MAXTARGETSIZE && target[i] != '\0'){ if(target[i] == strtofind[0]){ dectr = stfsiz; while(dectr>0){ if(target[i+dectr] == strtofind[dectr]){ if(dectr==0) return i; else dectr--; } else { i++; break; } } } else i++; } return -1; }
The logic here is that if the string you're searching for in the target is very long, it's probably a good idea to start at the end of it and 'search back' from where the end of the string being searched for would be, assuming i is the beginning of a match. This works on the assumption that it's more likely that you'll run into divergence between a match and a non-match further from the beginning of them than closer.
IE., I am assuming that while searching for, for example, "mushroom" in an arbitrary string of letters, that I'm less likely to encounter an m and then an o and another o while searching backward from the i+7th char than I will be to find an u, s, and h counting forward.
I know there are better ways to impliment this, but hopefully this will demonstrate the idea I have without making me have to work too much on an idea I'm not even sure is valid.



LinkBack URL
About LinkBacks


