04-Searching and Sorting Algorithms
04-Searching and Sorting Algorithms
Searching and sorting algorithms are algorithms applied to data structures. Being able to sort and then
search your data is a fundamental step before you can process and interpret your data.
Once you have your data in a list or array, you can apply sorting algorithms to rearrange it. Sorting
algorithms, such as quicksort, merge sort, and insertion sort, organize data within structures, making it
easier for searching algorithms to find data efficiently.
You use data search algorithms to find elements stored within your data structure. For data stored
linearly, using linear or binary searches is best. For nonlinear data structures, opting for graph traversal
algorithms is ideal.
Linear search algorithms search through data sequentially, starting with the first element and moving to
the next element in a line until finding the target. With binary search, the algorithms continually divide
the data in half until it reaches the target. For example, if you had a list of one to 10 and were searching
for a three, the algorithm would first divide the 10 digits into one to five and six to 10, realizing the three
fit into the one to five category. The algorithm would then divide one to 2.5 and 2.5 to five, realizing the
three is in the 2.5 to five category, and so on.
Graph traversal algorithms provide a way to move throughout nodes within a graph and find needed
information. When using a depth-first search (DFS), the algorithm explores a certain path of the graph as
far as it can before turning around and going back up the path. This method is more common to find
nodes farther away from the starting point.
When using a breadth-first search (BFS), the algorithm explores all nodes at a certain depth before
moving on to the next level. This method is more common to find nodes closer to the given source.