Search Algorithms#

TL;DR

Search algorithms are a fundamental concept in data structures and algorithms that are used to find an item in a collection of data. There are several types of search algorithms, each with its own strengths and weaknesses.

Linear search is a simple search algorithm that examines each element in a collection until the desired item is found. Linear search is easy to implement and works well for small collections or unsorted data, but it can be inefficient for large collections or when the desired item is near the end of the collection.

Binary search is a more efficient search algorithm that works on sorted collections. Binary search divides the collection in half at each step and eliminates one half of the remaining items based on the comparison with the target value. Binary search has a time complexity of \(O(log\ n)\), making it much faster than linear search for large collections.

Hashing is another search algorithm that works by using a hash function to map the search key to an index in an array. Hashing provides fast access to items with a time complexity of \(O(1)\), but it requires a good hash function and can suffer from collisions where multiple items map to the same index.

Tree-based search algorithms, such as binary search trees and balanced trees, provide efficient search operations with time complexity of \(O(log\ n)\). They are particularly useful for data that needs to be sorted, and can also be used for other operations such as insertion, deletion, and traversal.

The choice of search algorithm depends on the properties of the data and the specific requirements of the application. By choosing the most appropriate search algorithm, programmers can improve the efficiency and effectiveness of their programs.

Additional Resources
https://www.cdn.geeksforgeeks.org/wp-content/uploads/Binary-Search.png
Binary Search
  • repeatedly target the center of the search structure and divide the search space in half.

  • ex. binary search

  • note: specifically designed for searching in sorted data-structures…

https://blog.penjee.com/wp-content/uploads/2015/04/binary-and-linear-search-animations.gif
Sequential Search
  • the list or array is traversed sequentially and every element is checked.

  • ex. linear search

Linear v. Binary#

Linear

Binary

Input data need not to be in sorted.

Input data need to be in sorted order.

Also called sequential search.

Also called half-interval search.

\(T(n) = O(n)\)

\(T(n) = O(log\ n)\)

Multidimensional array can be used

Only single dimensional array is used

Performs equality comparisons

Performs ordering comparisons

Less complex.

More complex.

Very slow process.

Very fast process.