Lab Record Format

 

Program/output On The Left Page.

Aim, Description ,analysis and results on the Right Page

Linear Search

// Linear Search in C
#include <stdio.h>
int main() {
int arr[100], n, target, i, found = 0;

printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &target);

for(i = 0; i < n; i++) {
if(arr[i] == target) {
printf("Element found at position %d\n", i);
found = 1;
break;
}
}
if(!found) {
printf("Element not found\n");
}
return 0;
}

๐Ÿงช Sample Input and Output

๐Ÿ”น Sample Input 1 (Element Present at Beginning):Best Case

Enter the number of elements: 5
Enter the elements: 10 20 30 40 50
Enter the element to search: 10

๐Ÿ”น Output:

Element found at position 0

๐Ÿ”น Sample Input 2 (Element Present in the Middle):

Enter the number of elements: 6
Enter the elements: 5 15 25 35 45 55
Enter the element to search: 35

๐Ÿ”น Output:

Element found at position 3

๐Ÿ”น Sample Input 3 (Element Present at End):

Enter the number of elements: 4
Enter the elements: 2 4 6 8
Enter the element to search: 8

๐Ÿ”น Output:

Element found at position 3

๐Ÿ”น Sample Input 4 (Element Not Present):Worst Case

Enter the number of elements: 5
Enter the elements: 11 22 33 44 55
Enter the element to search: 99

๐Ÿ”น Output:

Element not found

 

Aim: To implement and understand the working of the Linear Search algorithm to search for an element in a list/array.

Description:

Linear Search, also known as sequential search, is a simple searching algorithm that checks each element of the list one by one until the desired element (called the key or target) is found or the list ends.

The algorithm begins at the first element and compares each element with the key:

·         If a match is found, the index of the element is returned.

·         If no match is found by the end of the list, the algorithm returns that the element is not present.

Linear search is straightforward and does not require the list to be sorted. It is most useful for small datasets or when the list is unsorted.

Step-by-Step Algorithm:

  1. Start
  2. Input the list (array) of elements
  3. Input the element to be searched (called target)
  4. Set a variable found to false
  5. Loop through each element in the list from index 0 to n-1:
    a. If
    list[i] == target:
        i. Set
    found = true
        ii. Print position
    i
        iii. Exit the loop
  6. If found == false, print "Element not found"
  7. Stop

 Analysis:

๐Ÿ” Time Complexity:

·         Best Case: O(1) → when the element is at the first position.

·         Worst Case: O(n) → when the element is not found or at the last position.

·         Average Case: O(n/2) ≈ O(n)

Space Complexity:

·         O(1) → as it uses a constant amount of extra space.

Limitations:

·         Inefficient for large lists compared to more advanced algorithms like Binary Search (which requires sorted data).

·         Performance degrades linearly as list size increases.

Advantages:

·         Very simple to implement.

·         Works on both sorted and unsorted lists.

Result:

The Linear Search algorithm was successfully implemented.
Given an array of elements and a target value, the algorithm correctly identified the position of the target element when present and displayed an appropriate message when the element was not found.

The working of the algorithm was verified using multiple test cases with:

·         Target at the beginning

·         Target in the middle

·         Target at the end

·         Target not present in the list

The output matched the expected results in all test cases.

 

 

Comments

Popular posts from this blog

Data Structures Lab PCCSL307 KTU 2024 Scheme - Dr Binu V P

Sparse Matrix - Transpose and Addition

Polynomial Addition using Arrays