c - Which is the better Searching algorithm ?? -
i have 3 groups of items(names). group 1 has around 2,1k items, group 2 - around 7,6k , group 3 around 21k. need search in these groups. need hint better. thought either put in bin tree:
gtree* t = g_tree_new((gcomparefunc)g_ascii_strcasecmp); , search this: goup = g_tree_lookup(t, (gpointer *)itemname);
or more efficient make 3 arrays of strings:
char g1[2300][14]; char g2[8000][14]; char g3[78000][14];
and search (not checked, pseudocode):
int isvalueinarray(char val, char *g[][14];, int size){ int i; (i=0; < size; i++) { if (memcmp(val, g[i], strlenth) == 0) return true; } return false; } int group=0; if (isvalueinarray(itemname, g2, 7800) ) group = 2; if (isvalueinarray(itemname, g1, 2300) ) group = 1;
or there better solution that?
if want asymptotically efficient way of finding whether string in set of strings can pre-processed, can consider using a trie. construction time linear (o(l), l sum of strings lengths in 3 sets), , lookup time linear on size of string looking (which is, in case, 14).
a binary search tree nice option, giving logarithmic (on size of string sets) performance. can bit slower, easier implement. note pre-processing (inserting strings of 3 sets tree) takes n * log(n) time, n sum of set sizes.
do not use linear search in array, it's slow.
Comments
Post a Comment