Linear Search Demonstration
Linear Search Demonstration
Linear Search (also known as sequential search) is the simplest search algorithm in data structures. It is used to find the location of a specific element in a list or array by checking each element one by one from the beginning until the desired element is found or the end of the list is reached.
How Linear Search Works
Start from the first element of the array.
Compare the target element with the current element.
If a match is found, return the index of the element.
If no match is found, move to the next element.
Repeat steps 2–4 until:
the element is found, or
the end of the array is reached.
If the element is not found, return -1 (or indicate that the element is not in the list).
Read More
Time Complexity
Best Case: O(1) → when the element is at the beginning.
Worst Case: O(n) → when the element is at the end or not present.
Average Case: O(n)
Space Complexity: O(1) → No extra space is used.
Advantages
Simple and easy to implement.
No need for the array to be sorted.
Works for both arrays and linked lists.
Disadvantages
Inefficient for large data sets.
Time-consuming compared to more advanced searching algorithms (e.g., binary search).