0% found this document useful (0 votes)
42 views6 pages

Algo Assignment-01 Linear SRCH

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)
42 views6 pages

Algo Assignment-01 Linear SRCH

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/ 6

ALGORITHM PRACTICAL ASSIGNMENTS (SOLUTIONS)

GROUP-A: Searching

Assign- 1) Linear Search Method (The program should report the number of
pass & comparisons)
PROCEDURE

Linear Search is defined as a sequential search algorithm that starts at one end and
goes through each element of a list until the desired element is found, otherwise the
search continues till the end of the data set.

How Does Linear Search Algorithm Work?


• Every element is considered as a potential match for the key and checked
for the same.
• If any element is found equal to the key, the search is successful and the
index of that element is returned.
• If no element is found equal to the key, the search yields “No match
found”.

ALGORITHM

1. A[n] input set


2. KEY is searching element

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 1


3. Flag=0
4. For i=1 to n
5. If a[i] == KEY
6. Msg→ Found and position is i
7. Flag=1
8. Break
9. End if
10. End for
11. If (!Flag)
12. Msg→ Not Found
13. Stop

PROGRAM

/* Linear Search */
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void LinearSearch(int a[], int n, int item)


{
int i, flag=0, com=0;

for (i = 0; i< n; i++)


{
com++;
if(a[i] == item)
{
flag = i+1;
break;
}
}
if(flag)
{
printf("\nItem founds at location: %d\n",flag);
printf("\nTotal Pass:1 Total Comparison:%d", com);
}
else
printf("\nItem not found\n");
}

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 2


void main()
{
clrscr();
FILE *in;
int n, i, Item, a[100];
cout<<"Enter Sample Size (No of Elements):";
cin>>n;
cout<<"Enter Searching Item:";
cin>>Item;

in=fopen("Algo\\Input.txt", "r");
i=0;
while(!feof(in))
{
fscanf(in, "%d", &a[i++]);
}
fclose(in);

LinearSearch(a, n, Item);
getch();
}

OUTPUT

Input.txt
1 2 …………..50

1st Round
Enter Sample Size: 50
Enter Searching Item: 10
Item Found and position is 10

2nd Round
Enter Sample Size: 50
Enter Searching Item: 100
Item not Found

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 3


DISCUSSION

Now, let's see the working of the linear search Algorithm.

To understand the working of linear search algorithm, let's take an unsorted array.
It will be easy to understand the working of linear search with an example.

Let the elements of array are -

Linear Search Algorithm


Let the element to be searched is K = 41

Now, start from the first element and compare K with each element of the array.

Linear Search Algorithm


The value of K, i.e., 41, is not matched with the first element of the array. So, move
to the next element. And follow the same process until the respective element is
found.

Linear Search Algorithm

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 4


Now, the element to be searched is found. So algorithm will return the index of the
element matched.

TIME & SPACE COMPLEXITY

Time Complexity:
• Best Case: In the best case, the key might be present at the first index. So
the best case complexity is O(1)
• Worst Case: In the worst case, the key might be present at the last index
i.e., opposite to the end from which the search has started in the list. So the
worst-case complexity is O(N) where N is the size of the list.
• Average Case: O(N)
Auxiliary Space: O(1) as except for the variable to iterate through the list, no other
variable is used.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 5


Literature Review

Advantages of Linear Search:


• Linear search can be used irrespective of whether the array is sorted or not.
It can be used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.

Drawbacks of Linear Search:


• Linear search has a time complexity of O(N), which in turn makes it slow
for large datasets.
• Not suitable for large arrays.

When to use Linear Search?


• When we are dealing with a small dataset.
• When we are searching for a dataset stored in contiguous memory.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 6

You might also like