 # Algorithms in Programming

Algorithms are commonly used tools in the IT world and a seemingly inescapable term in our everyday lives, thanks to social media. What is an algorithm? In computer science, an algorithm is a precise and finite list of rules or steps to perform an operation or solve a particular problem.

All algorithms work by taking inputs. Those inputs follow a defined set of steps in the algorithm to return an output, such as the content you see in your social media feed, or even something as simple as the answer to a math problem.

What are the benefits of using algorithms? Algorithms break down problems into digestible pieces. They have easy to follow step by step solutions to make a task easier to understand and accomplish.

What are the disadvantages of algorithms? Writing an algorithm takes time. While the flow of the algorithm needs to be precise and simple, turning that algorithm into code requires complex logic and programming knowledge.

## When We Use Algorithms

Technology aside, you’ve been using algorithms in your everyday life even before you had a cell phone. Maybe your parents had a morning routine when you were a child. The given problem to solve: Get a child ready for school. The set of instructions in your morning routine may look something like this:

Step 1. Wake up.

Step 2. Get dressed.

Step 3. Brush your teeth and hair.

Step 4. Have breakfast.

Output: A child ready for a day of learning!

That’s a simple algorithm.

When it comes to computer programming and software development, sets of algorithms tell the machine what to do and when to do it. Algorithms aren’t written in a programming language like Java or Python. They are written in pseudo code, and resemble a text based flowchart for the programmer to follow.

When people talk about “the algorithm” in regards to social media, it is a reference to the computer algorithms that make the decisions on which posts to show in your feed. The input for these algorithms is past interactions and activity you’ve made within the app or website.

What is an algorithm in C++ programming? While algorithms aren’t tied to specific programming languages, many languages have algorithmic functions in their standard libraries. C++ has 114 algorithms, including functions for sorting, binary search, and merge.

## Elements of an Algorithm

There are three basic elements for algorithms: sequencing, selections, and iteration

• Sequencing is the set of instructions to be used to get the result.

• Selection is when a decision is made in the instructions, usually a conditional statement (if/then, or if/then/else) or a binary decision (yes/no).

• Iteration repeats until a defined stopping point. Programmers often call iteration “loops” or “for loops” as the commands are looping over and over again until a condition is met.

To get an idea of these elements in a simple everyday algorithm, let's look at making toast.  The sequence would be to put the bread into the toaster, turn the toaster on, and remove the bread from the toaster. The selection element is how toasted you like your bread. If the bread is not toasted enough, then we move into the iteration element and repeat the steps until we arrive at the desired result.

## Characteristics of an Algorithm

Well-defined algorithms share a common set of characteristics.

• Unambiguous Operations: The instructions in the algorithm are precisely outlined. Each step is clearly outlined and independent of any programming language.

• Finiteness: The number of steps in the algorithm is finite.

• Input: Algorithms accept an input or set of defined inputs.

• Output: An output is produced at the end of the algorithm.

• Ordered: The steps are performed in a defined order, with each step affecting the following step.

• Feasibility: All steps should be effectively computable, meaning they are simple enough to adequately perform.

In addition to these common characteristics of algorithms, an algorithm should be modular, functional, user-friendly, and maintainable.

• Modular: It breaks down the input problem into smaller modules.

• Functional: The steps are logical.

• User-Friendly:Front-end developers and software engineers need to be able to understand the algorithm for the desired outcomes.

• Maintainable: An algorithm shouldn’t have significant output changes when inputs and steps are redefined. An example is the constant updates and changes to searching algorithms. Google redefines their algorithms 500-600 times a year and the end user barely notices!

## Types of Algorithms

Algorithms serve many purposes.  Different types of problems require different types of algorithms.  In computer programming algorithms are fundamental algorithms for accomplishing certain tasks. These include:

### 1. Randomized Algorithm

A random number is used to help decide the expected outcome. Randomized algorithms are simpler than other fundamental algorithms, but end up being far more efficient.

### 2. Brute Force Algorithm

This algorithm exhausts all possible solutions to find the correct outcome.  A brute force algorithm can be either optimizing, where it will select the best solution after finding every outcome, or sacrificing, where it will stop as soon as it finds the best solution. Brute force is often used to find the shortest path in mapping apps.

### 3. Greedy Algorithm

For an approach with less time complexity, greedy algorithms will run an iteration and choose the best result for that step and then move on to the next step.  This differs from the brute force algorithm that tries finding the best outcomes overall after all steps.

### 4. Divide and Conquer Algorithm

A step by step algorithm that divides problems into subproblems before solving.  The subproblems are solved first, and then the solutions are combined to provide the output for the original problem. This is one of the most common algorithms in programming.

### 5. Recursive Algorithm

The simplest subproblem is examined first, and then larger and larger problems are solved until the solution is obtained.

### 6. Dynamic Programming

By storing the solutions to subproblems only once, this efficient method of problem solving remembers past solutions and applies them to future calls. This avoids repetitive calculations and cuts the amount of time needed to solve the problem.

### 7. Searching Algorithm

This algorithm searches for elements in a data structure. In searching algorithms, elements can be retrieved through a sequential, also called linear search, where the algorithm goes through each element individually to find the solution. A faster way is an interval search, also called a binary search,  where the sorted data structure is repeatedly divided into two until the element is found.

### 8. Backtracking Algorithm

By following specified criteria, the path to the solution is built, unless it fails along the way.  If it fails, the algorithm backtracks to that point to build another solution.

### 9. Sorting Algorithm

Sorting algorithms group and rearrange data according to the steps in the algorithm. There is a comparison operator that decides the new order of the data structure.  Sorting algorithms can be further refined depending on how the algorithm is to sort the data.  Some popular variations of sorting algorithms include Bubble Sort, Merge Sort, Quick Sort, and Linear Sort.

Many of these fundamental algorithms have been further refined for specific tasks and purposes. Luckily there are numerous courses and tutorials available for anyone who wants to learn more. App developers need to have a solid understanding about the formation and analysis of algorithms for their projects.

## Looking for an algorithm expert?

Algorithms have become highly sophisticated. They are the driving force behind the technology for search engines, social media platforms, artificial intelligence, and machine-learning.

Is your company looking for a programming expert or product developer to guide your next algorithm focused project? MVP Match can connect your business to the top tech talent available from our freelancer community. 