This is a recursive definition, and we can make a recursive subroutine to implement it.
Previously, there were two pertimbahangan should we put in our calculation, which is a fact about recursive subroutine. First, the binary search algorithm begins by checking the "central element of a list". What happens if the list is empty? If no element in the list, then we are not likely to see elements in the middle. Or in other words, this is called "initial conditions" to check the element in the list, which has a list that is not empty.
What happened was we had to find value in the list empty? The answer is simple: We can say that the value we are looking for is not in the list. Empty list is the basic case for a binary search algorithm. Case basis for a recursive algorithm is a case that will be dealt with directly, not done recursively. Binary search algorithm has another base case, ie if we find the value we find in the middle of a list, then the program is completed. We do not need to do further recursion.
The second consideration is the subroutine parameters. In non-recursive subroutine binary search discussed earlier, the parameter is an array. However, in a recursive approach, we should be able to apply the subroutine recursively only part of the original list. In the original subroutine is non-recursive, we search the entire array, while the recursive subroutine should be looking at some of the array. Subroutine parameters should be able to tell in which part of the array will be searched.
Here is a recursive binary search algorithm to find a value in a section of an array of integers:
static int cariBiner (int [] A, int idksRendah, idksTinggi int, int value) {
/ / Look for the "value" in the array "A" from position "idksRendah" to "idksTinggi"
/ / The assumption is that an array sorted in ascending order
/ / If found return index value in array
/ / Where the value is located. If it does not return -1
if (idksRendah> idksTinggi) {
/ / This means that the list is empty because it should be lower idksRendah
/ / From idksTinggi. "Value" can not possibly exist in the list is empty.
return -1;
}
else {
/ / Check the elements in the list. If the value is equal to the content element
/ / Middle, then the return position.
/ / If not, search recursively at the beginning or end
/ / From the half-list
int idksTengah = (idksRendah + idksTinggi) / 2;
if (value == A [idksTengah])
idksTengah return;
else if (value A [idksTengah]
return cariBiner (A, idksTengah + 1, idksTinggi, value);
}
} / / End cariBiner ()
In the routine above, parameters and idksTinggi idksRendah declare the array to be searched. To search the entire array, we just need to call cariBiner (A, 0, A.length - 1, value). In either case basis - ie there is no element in the index ranges are given and when no value is found in the middle of that range - subroutine to give the answer directly, without recursion. In other cases, the subroutine will call itself recursively to calculate and return the results.
Tidak ada komentar:
Posting Komentar