Searching:
Search
is a process of finding a value in a list of values. In other words, searching
is the process of locating given value position in a list of values.
Most important
techniques used for searching are:
1) Linear Search
2) Binary Search
Linear Search
Algorithm (Sequential Search Algorithm):
Procedure:
Step
1:
Read the search element from the user
Step
2:
Compare, the search element with the first element in the list.
Step
3:
If both are matching, then display "Given element found!!!" and
terminate the function
Step
4:
If both are not matching, then compare search element with the next element in
the list.
Step
5:
Repeat steps 3 and 4 until the search element is compared with the last element
in the list.
Step
6:
If the last element in the list is also doesn't match, then display "Element not found!!!" and
terminate the function.
Example:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], n, i, Target;
clrscr();
printf("Enter size of
the list: ");
scanf("%d",
&n);
printf("Enter any %d
integer values: ",n);
for(i = 0; i < n; i++)
scanf("%d",
&a[i]);
printf("Enter the
element to be Search: ");
scanf("%d",
&Target);
for(i = 0; i < n; i++)
{
if(Target = = a[i])
{
printf("Element
is found at %d index", i);
break;
}
}
if(i = = n)
printf("Given
element is not found in the list!!!");
getch();
}
0 comments:
Post a Comment