What happens if a binary search on a non-sorted data set is attempted?
The results are unpredicable. If the data set has the target, it may or may not be found.
EDIT Just for kicks, I ran a little experiment. First, I picked an array size and generated an int array {0, 1, ..., size-1}. Then I shuffled the array, did a binary search for each value 0, 1, ..., size-1 and counted how many of these were found. For each size, I repeated the shuffle/search-for-each-value steps 100,000 times and recorded the percent of searches that succeeded. (This would be 100% for a sorted array.) The results are (rounded to the nearest percent):
Size % Hit
10 34%
20 22%
30 16%
40 13%
50 11%
60 10%
70 9%
80 8%
90 7%
100 6%
So the larger the array, the worse the effects of not sorting. Even for relatively small arrays, the results are pretty drastic.
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