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
Case | Complexity | Explanation |
---|---|---|
π’ Best Case | Element is found at the first position | |
π‘ Average Case | On average, half the elements are checked | |
π΄ Worst Case | Element 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.
π 5. Variations of Sequential Search
πΉ 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.
π 6. When to Choose Sequential Search?
Scenario | Best 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!