With respect to an array containing a lot of duplicate elements, is there any operations to improve the performance of normal binary search?
You can create two arrays. One for values, and the other for repetitions. Then, you can search the values array using binary search.
AraK's answer is 'the only' answer. In general you can't improve worst-case performance on any array with duplicates. Let's say your duplicates tend to concentrate on the end of the array. Now you're looking for an element towards the beginning: your search will have to run as usual.
If you know that duplicates tend to appear on one side more often, you can skew the splitting to improve average performance. If they appear towards the end, e.g., you can do the first split at 1/3 instead of 1/2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With