Unit 3
Unit 3
Searching:
➢ Searching is the process of finding the location of given element in the linear array.
➢ The search is said to be successful if the given element is found, i.e., the element does exist in
the array; otherwise unsuccessful.
➢ There are two searching techniques :
a. Linear search (sequential)
b. Binary search
➢ The algorithm which we choose depends on organization of the array elements.
➢ If the elements are in random order, then we have to use linear search technique, and if the
array elements are sorted, then it is preferable to use binary search.
External searching: When the records are stored in disk, tape, any secondary storage
then that searching is known as ‘External Searching’.
Internal Searching: When the records are to be searched or stored entirely within the
computer memory then it is known as ‘Internal Searching’.
Sequential search:
Linear search or sequential search is a method for finding a particular value in a list that
consists of checking every one of its elements, one at a time and in sequence, until the desired
one is found.
Linear search is the simplest search algorithm; it is a special case of brute-force search. Its
worst case cost is proportional to the number of elements in the list.
1. Repeat For J = 1 to N
2. If (ITEM == A[J]) Then
3. Print: ITEM found at location J
4. Return [End of If]
[End of For Loop]
5. If (J > N) Then
6. Print: ITEM doesn’t
exist
[End of If]
7. Exit
Program:
#include <stdio.h>
void main()
{
int a[10],i,n,m,c=0, x;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array:\n ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the number to be search:\n ");
scanf("%d",&m);
for(i=0;i<n;i++)
{
if(a[i]==m)
{
x=i;
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found at location %d", x);
}