Algo Assignment-01 Linear SRCH
Algo Assignment-01 Linear SRCH
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.
ALGORITHM
PROGRAM
/* Linear Search */
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
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
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.
Now, start from the first element and compare K with each element of the array.
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.