Search Algorithm - Sequential Search And Binary Search
Last updated on September 13, 2023 pm
Sequential Search
Definition
Sequential Search, also known as Linear Search, is a straightforward searching algorithm used to find a specific element within a collection of elements, such as an array or a list. It works by examining each element one by one, starting from the beginning, until either the target element is found or the entire collection has been traversed. Sequential Search is easy to understand and implement but has a linear time complexity of O(n) in the worst case, making it less efficient for large datasets compared to more advanced search algorithms like Binary Search for sorted arrays.
Code
1 |
|
Binary Search
Definition
Binary Search is a highly efficient search algorithm used to find a specific element within a sorted collection of elements, such as an array. It works by repeatedly dividing the search interval in half and comparing the middle element with the target element. If the middle element matches the target, the search is successful. If the target is smaller, the search continues in the left half of the interval; if it’s larger, the search continues in the right half. Binary Search has a time complexity of O(log n) in the worst case, making it significantly faster than linear search algorithms for large datasets, but it requires that the data be sorted prior to searching.
Code
1 |
|