Department of Computer Science & Engineering, DES’s COET Dhamangaon Rly.
PRACTICAL NO.1: Write a program to implement Linear Search
Date: 20/07/2017
EXPERIMENT NO: 01
1. EXPERIMENT VISION: Write a program to implement Linear Search.2. OBJECTIVE: Program to implement an array of object in C and to find the specific
element.
3. THEORY:Linear search is usually very simple to implement, and is practical when the list
has only a few elements, or when performing a single search in an unordered list. many values
have to be searched in the same list, it often pays to pre-process the list in order to use a faster
method. For example, one may sort the list and use binary search, or build any efficient search
data structure from it. Should the content of the list change frequently, repeated re-organization
may be more trouble than it is worth. As a result, even though in theory other search algorithms
may be faster than linear search (for instance binary search), in practice even on medium sized
arrays (around 100 items or less) it might be infeasible to use anything else. On larger arrays, it
only makes sense to use other, faster search methods if the data is large enough, because the
initial time to prepare (sort) the data is comparable to many linear searches. Linear search in c
programming: The following code implements linear search (Searching algorithm) which is used
to find whether a given number is present in an array and if it is present then at what location it
occurs. It is also known as sequential search. It is very simple and works as follows: We keep on
comparing each element with the element to search until the desired element is found or list
ends. Linear search in c language for multiple occurrences and using function.
4.Algorithm:
Step-1: Start
Step-2: Accept no. of the values
Step-3: Accept the values and store it in an array
Step-4: Accept the no. to be searched as ‘num’
Step-5: If a value is found equal to ‘num’, output, ‘Element found’.
Step-6: If the value is not found, output, ‘Element not found’.
Step-7: Stop
5.Flowchart:
6. PROGRAM:
#include<stdio.h>
void main()
{
int arr[100],search,n,i;
printf("enter the no. element to be enter in array");
scanf("%d",&n);
printf("enter the array element");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("enter the element to be search");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(arr[i]==search)
{
printf("element %d search at the position %d",search,i+1);
break;
}
}
if(arr[i]!=search)
printf("search %d element is not found",search);
}
7. OUTPUT:
------------------------------------------
------------------------------------------
8. CONCLUSION:
Thus a we have studied C program for linear search and executed successfully.
9. REMARK:
Array of element is the concepts are used for printing the values of number
element and to find the element using implementing linear search method.
10. DISCUSSION QUESTIONS:
1)What is array? How are they created?
______________________________________________________________________________
___________________________________________________________________________
2) What are the different types of array?
______________________________________________________________________________
__________________________________________________________________________
3) What is the mean complexity? What are the types of complexity?
______________________________________________________________________________
______________________________________________________________________________
4) What is the complexity of linear search method?
______________________________________________________________________________
______________________________________________________________________________

No comments:
Post a Comment