# Top 8 Algorithms Every Programmer Should Learn

Given the technological changes happening around us, it’s recommended to keep yourself updated, especially when you are pursuing a career in the technological domain. While standard algorithms are to be mastered, you cannot ignore what’s in trend now. So, what is one supposed to do? Well, we will tell you. Veganab has compiled a list of 8 algorithms that every programmer should know in order to match pace with the dynamic changes happening in the field of the programming language.

## 1. Selection, Bubble, and Insertion Sort

Selection, Bubble, and Insertion sort are some of the first that new developers should work through. In any scenario when speed matters you’re not going to be using these algorithms but working with them is a great introduction to array traversal and manipulation. Selection sort helps in finding the minimum element in the unsorted portion of the array and swapping it with the first unsorted element. Repeat until the array is fully sorted. Bubble sort iterates through the array repeatedly, comparing adjacent pairs of elements and swapping them if they are in the wrong order. Repeat until the array is fully sorted. Insertion sort, on the other hand, helps to build up a sorted subarray from left to right by inserting each new element into its correct position in the subarray. Repeat until the array is fully sorted.

## 2. Binary Search

Binary search is among the first things taught in any computer science class. Binary search is used to perform a very efficient search on sorted datasets. The time complexity is O(log2N). A binary search consists of taking a sorted array, iteratively splitting the array into two, and comparing an element that you are looking for against each half until you find the element. The idea is to repeatedly divide in half the portion of the list that could contain the item until we narrow it down to one possible item. A common application of this algorithm is when you search for the name of the song in a sorted list of songs, it performs binary search and string-matching to quickly return the results. It is perhaps the simplest example of how a little bit of ingenuity can make things, quite literally, exponentially more efficient

**Huffman Coding**

Huffman coding is the foundation of modern text compression. It works by considering how often different characters appear in a text and organizing them in a tree based on this frequency. Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length codes to input characters, lengths of the assigned codes are based on the frequencies of corresponding characters. The variable-length codes assigned to input characters are Prefix Codes, which means the codes (bit sequences) are assigned in such a way that the code assigned to one character is not the prefix of code assigned to any other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding the generated bitstream.

Let us understand prefix codes with a counter-example. Let there be four characters a, b, c, and d, and their corresponding variable length codes be 00, 01, 0, and 1. This coding leads to ambiguity because the code assigned to c is the prefix of codes assigned to a and b. If the compressed bit stream is 0001, the de-compressed output may be “cccd” or “ccb” or “acd” or “ab”. There are mainly two major parts in Huffman Coding – to build a Huffman Tree from input characters and to traverse the Huffman Tree and assign codes to characters.

**Quicksort and Mergesort**

Sorting is the most heavily studied concept in computer science. Idea is to arrange the items on a list in a specific order. Though every major programming language has built-in sorting libraries, it comes in handy if you know how they work. Depending upon the requirement you may want to use the merge sort, quick sort, bucket sort, heap sort, or counting sort.

More importantly, one should know when and where to use them. This makes sorting algorithms one of the most fundamental tools that a developer should have in their arsenal. While sorting algorithms are great for getting comfortable with arrays, Quicksort and Mergesort are efficient enough to be used in serious applications. Being comfortable implementing these sorting algorithms these algorithms are essential to being a serious developer.

## 5. Gradient Descent

Now for a lot of developers, Gradient Descent is not necessarily going to be useful as far as any programming language is concerned. If, however, you are touching anything with regression or machine learning, Gradient Descent is going to be at the heart of your work. Gradient Descent is known as one of the most commonly used optimization algorithms to train machine learning models by means of minimizing errors between actual and expected results. Further, gradient descent is also used to train Neural Networks. In mathematical terminology, the Optimization algorithm refers to the task of minimizing/maximizing an objective function f(x) parameterized by x. Similarly, in machine learning, optimization is the task of minimizing the cost function parameterized by the model’s parameters. The main objective of gradient descent is to minimize the convex function using iteration of parameter updates. Once these machine learning models are optimized, these models can be used as powerful tools for Artificial Intelligence and various computer science applications. In short, gradient descent is a method of procedure optimizing functions using calculus. In the context of regression and machine learning, this means finding specific values that minimize the error in your prediction algorithm. While it is certainly more mathematically involved than a lot of these other algorithms if you are working significantly with data and predictions, understanding how gradient descent works is incredibly important.

## 6. Breadth-First Search (BFS)

Breadth-First Traversal (or Search) for a graph is similar to the Breadth-First Traversal of a tree. The only catch here is, that, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we divide the vertices into two categories: visited and not visited. A boolean visited array is used to mark the visited vertices. For simplicity, it is assumed that all vertices are reachable from the starting vertex. BFS uses a queue data structure for traversal. Again, trees turn out to be at the heart of many algorithms and software that developers work with. As such, understanding basic tree traversal is a top priority for an aspiring developer. Breadth-first search works by exploring a tree level by level until the target node is found.

## 7. Depth First Search (DFS)

Depth-first search is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. So the basic idea is to start from the root or any arbitrary node and mark the node and move to the adjacent unmarked node and continue this loop until there is no unmarked adjacent node. Then backtrack and check for other unmarked nodes and traverse them. Finally, print the nodes in the path. Continuing with tree traversal, Depth-First Search is the other main approach for finding an element in a tree. Instead of working down the tree level by level, it explores the tree branch by branch. Now assuming it does not have infinitely extended branches, DFS will similarly always work. Implementing these two search algorithms isn’t particularly complex, but what is incredibly important is learning when to use one over the other. A lot of software design is being able to understand the structure of the information you are working with and pick algorithms that optimize for that structure.

## 8. Dijkstra’s Algorithm

Another incredibly important issue that developers work with is pathfinding via a variety of programming languages is this. Dijkstra’s Algorithm basically starts at the node that you choose (the source node) and it analyzes the graph to find the shortest path between that node and all the other nodes in the graph. The algorithm keeps track of the currently known shortest distance from each node to the source node and it updates these values if it finds a shorter path. Once the algorithm has found the shortest path between the source node and another node, that node is marked as “visited” and added to the path.

The process continues until all the nodes in the graph have been added to the path. This way, we have a path that connects the source node to all other nodes following the shortest path possible to reach each node. Graphs turn out to be an incredibly versatile way to describe all kinds of problems that involve networks of distinct objects. Dijkstra’s algorithm is a way of finding the quickest path between two nodes in a graph. It is the foundation of most work done in path-finding and finds itself used in anything from artificial intelligence to game design. It helps you to execute web crawling. In the longer run, it can help in building bots such as chess bots via Artificial Intelligence (AI). Best of all, it can enhance navigation by manifolds letting you find the shortest path between two cities on a map.