Sie sind auf Seite 1von 9

1.

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:

Figure: Binary Search

2.1: Recursive Binary Search

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 (Recursive Binary search)


int binary_search(int A[ ], int key, int imin, int imax)
if (imax < imin)
return KEY_NOT_FOUND;
else
{
int imid = midpoint(imin, imax);
if (A[imid] > key)
return binary_search(A, key, imin, imid - 1);
else if (A[imid] < key)
return binary_search(A, key, imid + 1, imax);
else
return imid;
}
}

2.2: Iterative Binary search:

1.The action or a process of iterating or repeating in a procedure at which repetition of


a sequence of operations yields results successively closer to a desired result.
2. Like recursion method it also calls size of the array, all the numbers of the array and
the number to be found.
3. Then it uses the divide and conquer method to reduce the whole program into sub
program. i.e. The repetition of a sequence of computer instructions a specified number
of times or until a condition is met.

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;
}

3.0: Flow Chart

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)

4.2: Iterative binary search:


The integers are: 13, 15, 18, 20, 22.
Min

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

structure; recursion achieves repetition through repeated method calls.


4. Iteration terminates when the loop-continuation condition fails; recursion
terminates when a base case is recognized.

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.

6. Iterative binary search takes less time than Recursion search.

6.0 Appendix source code:

Recursive Binary search


#include<stdio.h>
binarysearch(int a[],int n,int min,int max)
{ int mid;
if (min > max)
return -1;
mid = (min + max)/2;
if(n == a[mid])
{ printf("The element is at position %d\n",mid+1);
return 0;
}
if(n < a[mid])
{ max = mid - 1;
binarysearch(a,n,min,max);
}
if(n > a[mid])
{ min = mid + 1;
binarysearch(a,n,min,max);
}
}
main()
{ int a[50];
int n,no,x,result;
printf("Enter the number of terms : ");
scanf("%d",&no);
printf("Enter the elements :\n");
for(x=0;x<no;x++)
scanf("%d",&a[x]);
printf("Enter the number to be searched : ");
scanf("%d",&n);
result = binarysearch(a,n,0,no-1);
if(result == -1)
printf("Element not found");
return 0;
}

# Source Code

Iterative binary search


#include<stdio.h>
#include<conio.h>
main()
{
int c, max, min, mid, n, search, array[100];
printf("Enter number of elements: ");
scanf("%d",&n);
printf("\nEnter %d integers in sorted sorted :\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("\nEnter value to be searched in above list : ");
scanf("%d",&search);
max = 0;
min = n - 1;
mid = (max+min)/2;
while( max <= min )
{
if ( array[mid] < search )
max = mid + 1;
else if ( array[mid] == search )
{
printf("\n%d found at position %d.\n", search, mid+1);
getch();
break;
}
else
min = mid - 1;
mid = (max + min)/2;
}
if ( max > min )
printf("Not found! %d is not present in the above list.\n", search);
getch();
return 0;
}

Das könnte Ihnen auch gefallen