Note that Java provides the interface that contains a single method, compareTo. Given an ordered collection of elements as an array, the following program develops a class BinarySearch that contains a parameterized static method binarySearch for any base type T. These keys are totally ordered, which means that given two keys, k i and k j, either k i k j. Each element A has a key value k i that can be used to identify the element. The input to binary search is an indexed collection A of elements. We keep going in the same way until the item is found or it is determined that the sought-for element is not present in the list. It cuts the number of elements to be compared by half. If so, we keep looking in the first half of the list otherwise, we look in the second half. If not, than it compares the sought-for element with the middle element of the list and check whether the middle element is larger than the element we are trying to find. Binary search looks at the middle element of the list and see if the element sought-for is found.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |