in reply to Re: Modified Binary Search
in thread Modified Binary Search
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Modified Binary Search
by salva (Canon) on Jan 14, 2010 at 09:05 UTC | |
That makes an O(log N) algorithm into O(N). Obviously it does not! As for any binary search algorithm you have a previous step where you build the array, sorting something, loading it from disk, whatever. This operation is O(N) in the best case and usually O(NlogN) because of the sorting. Once that array is build you can perform binary searches over it with cost O(logN). In order to amortize the cost of building the array the number of binary searches has to be high (otherwise, there are better algorithms). The algorithm I have proposed increments the cost of the setup step but does not change its complexity because it already was O(N) at best. Also, if the number of repeated strings is high, @start would be considerably smaller than @a and so less operations will be performed per binary search. That means that if the number of binary searches to be carried out is high enough, my algorithm will actually be faster than any other one attacking @a directly. | [reply] [d/l] [select] |
|
Re^3: Modified Binary Search
by salva (Canon) on Jan 14, 2010 at 14:32 UTC | |
The parameters in the benchmarks are:
Note also than this code only looks for the lowest index where some given string is found. Handling the case described by the OP where he also needs to find the highest index is trivially handled in my algorithm without increasing its computation cost but will require an additional binary search when using the naive algorithm. Here are the results I have gotten on my machine: Read more... (4 kB) | [reply] [d/l] [select] |
by ikegami (Patriarch) on Jan 14, 2010 at 23:10 UTC | |
Of course the array construction won't add to the time if you don't include it in the code that's timed. You assume the same list is always searched. I suppose a more precise analysis is O(log(N) + N/M) where M is the number of searched performed between modifications of the list. The number of such searches have to be proportional to the size of the list to get less than O(N). | [reply] |