## The Mastery of Computer Programming: Primary Algorithms - Sykalo Eugene 2023

# Searching

Sorting and Searching

## Linear Search

Linear search is one of the simplest search algorithms used for finding an element within a list or array. It works by traversing the list or array sequentially and comparing each element with the target element until a match is found or the end of the list or array is reached.

The algorithm starts at the beginning of the list or array and compares each element with the target element. If the element is not equal to the target, the algorithm moves to the next element and repeats the process. If the element is equal to the target, the algorithm stops and returns the index of the matching element.

Linear search is easy to understand and implement, and it works with unsorted and sorted lists or arrays. However, it has a time complexity of O(n), where n is the number of elements in the list or array. This means that the algorithm becomes slower as the size of the list or array increases.

## Binary Search

Binary search is a more efficient algorithm for searching an ordered list or array. It works by repeatedly dividing the search interval in half until the target element is found or the search interval is empty.

The algorithm starts by comparing the target element with the middle element of the list or array. If the target element is less than the middle element, the algorithm discards the upper half of the search interval and continues the search on the lower half. If the target element is greater than the middle element, the algorithm discards the lower half of the search interval and continues the search on the upper half. The process repeats until the target element is found or the search interval is empty.

Binary search has a time complexity of O(log n), where n is the number of elements in the list or array. This means that the algorithm becomes faster as the size of the list or array increases. Binary search is particularly useful for large or ordered lists or arrays where the cost of sorting the list or array is lower than the cost of performing a linear search.

However, binary search requires that the list or array is sorted in ascending or descending order before the search begins. Sorting a list or array can be time-consuming, and the cost of sorting may outweigh the benefits of using binary search in certain situations.

## Hash Search

Hash search is a data structure used for efficient searching of a particular value from a collection of values. It works by mapping the values to a unique index in a hash table, which allows for constant-time access to the value.

The hash function is used to calculate the index of the value in the hash table. The hash function takes the value as input and returns an index in the hash table. The hash function should produce the same index for the same value every time it is called. However, different values may produce the same index, which is known as a hash collision.

To resolve hash collisions, a collision resolution technique is used. One common technique is chaining, where each index of the hash table contains a linked list of values that have the same index. When a hash collision occurs, the value is added to the linked list at the corresponding index.

Hash search has a time complexity of O(1), which means that the time required to search for a value is constant, regardless of the number of values in the collection. This makes hash search highly efficient for large collections of values.

However, hash search has some limitations. It requires a good hash function that can distribute the values evenly across the hash table to avoid collisions. Also, the size of the hash table needs to be carefully chosen to balance the number of values and the number of hash collisions.

Hash search is widely used in computer programming and data processing, particularly in database management systems and indexing systems. It is also used in cryptography and security systems for data encryption and decryption.

## Tree Search

Tree search is a powerful algorithm for searching hierarchical data structures, such as trees and graphs. It works by traversing the nodes of the tree in a systematic way to find the desired node or nodes.

The algorithm starts at the root node of the tree and compares the value of the node with the target value. If the value of the node is equal to the target value, the algorithm stops and returns the node. If the value of the node is less than the target value, the algorithm moves to the right child of the node and repeats the process. If the value of the node is greater than the target value, the algorithm moves to the left child of the node and repeats the process. The process continues until the target value is found or the algorithm reaches a leaf node, which does not have any children.

Tree search has a time complexity of O(log n), where n is the number of nodes in the tree. This means that the algorithm becomes faster as the size of the tree increases. Tree search is particularly useful for large and complex data structures, such as databases and file systems.

However, tree search requires that the tree is organized in a specific way, such as a binary search tree, where the values of the left child are less than the value of the parent node, and the values of the right child are greater than the value of the parent node. If the tree is not organized in this way, the algorithm may not work correctly or may not be efficient.

## Graph Search

Graph search is a complex algorithm for searching graphs or networks. It works by traversing the nodes and edges of the graph in a systematic way to find the desired node or nodes.

The algorithm starts at a specific node of the graph and explores its neighbors. It then moves to the next unexplored neighbor and repeats the process. The algorithm continues until the desired node is found or all nodes have been explored.

There are two main types of graph search algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS).

BFS starts at the initial node and explores all the neighboring nodes before moving to the next level of nodes. It continues this process until the desired node is found or all nodes have been explored. BFS is useful for finding the shortest path between two nodes in an unweighted graph.

DFS starts at the initial node and explores as far as possible along each branch before backtracking. It continues this process until the desired node is found or all nodes have been explored. DFS is useful for finding paths between nodes in a weighted graph and for detecting cycles in a graph.

Graph search has a time complexity of O(E + V), where E is the number of edges and V is the number of vertices in the graph. This means that the algorithm becomes slower as the size of the graph increases.

Graph search is particularly useful for solving problems in computer science, such as pathfinding, network analysis, and data clustering. It is also used in social networks, recommendation systems, and web search engines.