Beruflich Dokumente
Kultur Dokumente
0 : Objectives:
1. To understand the searching technique concept and the purpose of searching
operation.
2. To understand the implementation of basic searching algorithm through
recursive and iterative method.
3. Able to analyze the efficiency of the searching technique.
4. Able to implement searching technique in problem solving.
2.0: Description:
Recursive binary search is an implementation of the binary search algorithm that uses
recursive method calls
1. The initial call uses the indices of the entire array to be searched.
2. The procedure then calculates an index midway between the two indices,
determines which of the two sub arrays to search, and then does a recursive call to
search that sub array. The variables min and max are the lowest and highest
inclusive indices that are searched.
3. With unlimited numbers, the midpoint can be calculated as "(min + max) / 2". In
practical programming, however, the calculation is often performed with numbers of
a limited range, and then the intermediate result "(min + max)" might overflow.
4. With limited numbers, the midpoint can be calculated correctly as "min + ((max imin)/2)".
Algorithm
int binary_search(A[], int key,min, max)
{
while (max >= min)
{
int mid = midpoint(min, max);
if(A[imid] == key)
return imid;
else if (A[imid] < key)
imin = imid + 1;
else
imax = imid - 1;
}
return KEY_NOT_FOUND;
}
4.0 Analysis:
4.1: Recursive binary search
The Elements: 14, 17, 23, 24, 25
Min
Max
mid
A[mid]
0
3
4
4
4
4
2
3
4
17
23
24 (Found)
Max
Mid
A[mid]
0
3
4
4
2
3
15
18
5.0 Discussion:
1. The binary search algorithm begins by comparing the target value to value of the
middle element of the sorted array. If the target value is equal to the middle
element's value, the position is returned. If the target value is smaller, the search
continues on the lower half of the array, or if the target value is larger, the search
continues on the upper half of the array. This process continues until the element
is found and its position is returned, or there are no more elements left to search
for in the array and a "not found" indicator is returned.
2. Both iteration and recursion are based on a control structure: Iteration uses a
repetition structure; recursion uses a selection structure.
3. Both iteration and recursion involve repetition: Iteration explicitly uses a repetition
5. Iteration and recursion can occur infinitely: An infinite loop occurs with iteration if
the loop-continuation test never becomes false; infinite recursion occurs if the
recursion step does not reduce the problem in a manner that converges on the
base case.
# Source Code