Definition:
Sequential Search (also called Linear Search) is a basic algorithm used to find a specific element in a collection by checking each element one by one.
It is most effective for small datasets or unsorted data.


πŸ“Œ 1. Purpose and Use Cases

πŸ”Ή When to use Sequential Search?
βœ… When the dataset is unsorted.
βœ… When the dataset is small.
βœ… When there is no efficient index structure to optimize searching.

πŸ”Ή When NOT to use it?
❌ When working with large datasets (better alternatives exist, like Binary Search or Hash Tables).


πŸ“Œ 2. Implementation

πŸ“Œ Iterative Implementation in Java

public class SequentialSearch {
    public static int search(int[] array, int key) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == key) {
                return i; // Return index if found
            }
        }
        return -1; // Return -1 if not found
    }
 
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        int key = 30;
        
        int result = search(numbers, key);
        if (result != -1) {
            System.out.println("Element found at index: " + result);
        } else {
            System.out.println("Element not found.");
        }
    }
}

πŸ“Œ 3. Complexity Analysis

πŸ“Œ Time Complexity

CaseComplexityExplanation
🟒 Best CaseElement is found at the first position
🟑 Average CaseOn average, half the elements are checked
πŸ”΄ Worst CaseElement is at the last position or not found

πŸ“Œ Space Complexity

  • β†’ Uses only a few extra variables, making it memory-efficient.

πŸ“Œ 4. Advantages and Disadvantages

πŸ’‘ Pros
βœ”οΈ Simple to implement.
βœ”οΈ Works on both sorted and unsorted lists.
βœ”οΈ Does not require additional memory.

⚠️ Cons
❌ Inefficient for large datasets.
❌ Slower than Binary Search when searching in a sorted dataset.
❌ Not suitable when frequent searches are needed in a large dataset.


πŸ”Ή Sentinel Search: Improves performance by placing a β€œsentinel” at the end to eliminate extra boundary checks.
πŸ”Ή Self-Organizing Search: Moves frequently searched elements to the front for better future performance.


ScenarioBest Algorithm
πŸ” Small datasetβœ… Sequential Search
πŸ“‹ Unsorted dataβœ… Sequential Search
🏎️ Sorted data❌ Binary Search
🏒 Large dataset❌ Hashing or Tree-based Search

πŸ“Œ 7. Summary

πŸ”Ή Sequential Search is the simplest searching method, but not efficient for large datasets.
πŸ”Ή Suitable for small, unsorted collections where search operations are infrequent.
πŸ”Ή Time Complexity: , Space Complexity: .


πŸš€ Want to learn more?
Next, check out Binary Search for an optimized searching method for sorted lists!