Binary Search algorithm

To find a record, I’m using binary search. My question is not about whether or not the data is present. My questions are listed here.

  1. if data are in following order

1 2 3 4 4 5 5 5 6 7 8 8 9 10 11

If I look for 5 where it is located first. I can determine whether or not the number 5 is there by utilising a binary search technique. In the preceding situation, as demonstrated in this post, I may obtain the 6th location of 5 (a total of 13 data). But I need to obtain the fifth position. How do I obtain this using binary search? In some circumstances, I need to obtain the final position of the provided data. Where can I obtain this binary search algorithm?

Is there a way faster than binary search? But what about the hashing method?

The binary search is one of the fastest when searching an ordered collection. If the collection allows duplicate values then when a hit is found you need to check the elements either side for the duplicates.

Interesting because you have not explained the difference between the three elements that contain the value ‘5’. In other words what makes one ‘5’ different from another?

A HashMap is a faster method but only if the element keys are unique. So you couldn’t store ‘5’ three times in a hash map.

We really need more information about the actual data you are storing because it will help determine the best data structure / sort algorithm to use.

Java already has a binary search method - see here for more info

2 Likes