0% found this document useful (0 votes)
20 views3 pages

Unit 3

Nots of data structure

Uploaded by

kimjohn2331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Unit 3

Nots of data structure

Uploaded by

kimjohn2331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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.

How Linear Search works

Linear search in an array is usually programmed by stepping up an index variable until it


reaches the last index. This normally requires two comparisons for each list item: one to check
whether the index has reached the end of the array, and another one to check whether the item
has the desired value.

Linear Search Algorithm

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

Complexity of linear Search


Linear search on a list of n elements. In the worst case, the search must visit every element
once. This happens when the value being searched for is either the last element in the list, or is
not in the list. However, on average, assuming the value searched for is in the list and each list
element is equally likely to be the value searched for, the search visits only n/2 elements. In
best case the array is already sorted i.e O(1)

Algorithm Worst Case Average Case Best Case


Linear Search O(n) O(n) O(1)

You might also like