in reply to Re^2: performance issues with grepping large sorted arrays
in thread performance issues with grepping large sorted arrays
I absolutely agree that a binary search would be faster if you have a sorted array (though sorting the array in the first place would also take quite some time, and I was under the impression that the OP would only be doing that in an attempt to speed up things).
As to "the worst case is still O(n) and the average case is O(n/2) which simplifies to O(n)": I think in most real life situations, you're very unlikely to encounter the worst case scenario (in the OP's case, it would mean that every $key being looked up would match the last element of the array, or is not found at all). Sure, ultimately it's a statistical issue, but in the typical case there will be some improvement when short-circuiting the search. But that's a moot point anyway, as there are even better alternatives... (like hash tables, with constant-time lookup on average)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: performance issues with grepping large sorted arrays
by gam3 (Curate) on Jul 24, 2007 at 14:25 UTC | |
by mr_mischief (Monsignor) on Jul 24, 2007 at 16:43 UTC | |
by almut (Canon) on Jul 24, 2007 at 17:35 UTC | |
by mr_mischief (Monsignor) on Jul 24, 2007 at 20:13 UTC |