Search
Linear Search
Linear search is a simple method to find an item in a list. It checks each item one by one until it finds the target or reaches the end. This search does not need the data to be sorted.
For example, if you are looking for a name in a list, you start at the first name and check each one until you find the right one. If the name is at the end of the list, it will take longer to find.
Linear search is easy to understand and use. However, it can be slow if the list is long. If there are many items, checking each one takes time. In the worst case, the search needs to check every item before finding the target.
This method works well for small lists or when the data is not sorted. But for larger lists, other search methods are faster.
Binary Search
Binary search is a faster way to find an item in a sorted list. It works by cutting the list in half repeatedly until it finds the target.
For example, if you have a list of numbers from 1 to 100 and you want to find 75, binary search first checks the middle number. If the middle number is too small, it looks at the higher half of the list. If it is too big, it looks at the lower half. This process continues until the item is found.
Binary search is much faster than linear search because it does not check every item. Instead, it removes half of the remaining items each time. This makes it very efficient for large lists.
However, binary search only works when the data is sorted. If the list is not sorted, binary search cannot be used.
Hash Search
Hash search is a very fast way to find data using a hash table. A hash table is a special structure that stores data using a key-value system. Instead of searching through a list, hash search uses a hash function to calculate the location of the item.
For example, if you want to store and find names, a hash function will convert each name into a number that represents its location in memory. When searching for a name, the system checks only that location instead of looking through all names.
Hash search is much faster than linear search and binary search. It can find items in almost constant time, no matter how large the data set is. However, hash tables require extra memory, and hash functions must be designed carefully to avoid collisions—when two items get the same location.
Despite these challenges, hash search is widely used in databases and programming because of its high speed.