Algorithms

An algorithm is a set of steps / instructions that leads to solving a problem. Algorithms are a fundamental part of computer science. They are used to accomplish a variety of computational tasks, such as performing calculations, or finding specific information in databases.

How to make tea

Step 1: Boil one cup of water

Step 2: Add one teaspoon of tea leaves (with sugar if desired)

Step 3: if you want milk, add it at the 3 minute mark

Step 4: Heat tea to boiling point

Step 5: Pour into cup

We may not realize it, we also use algorithms in our daily lives. Our rituals after waking up, the specific way we choose to make our coffee, the way we fold our laundry, all these tasks are completed as a result of us following an algorithm which we have chosen to execute.

Characteristics of an algorithm

  • Well-defined instructions
  • Finite number of steps
  • Zero or more input(s)
  • One or more output(s)
  • Consistent

They can be expressed as natural languages, programming languages, pseudo code, flowcharts and control tables.

After we have chosen the representation of data as required, this data has to be processed somehow. This is what leads to the need of algorithms. The process of interest could be searching, sorting, encryption, etc. We will cover storing, searching, and sorting algorithms in detail, as they underlie much of computer science.

Types of Algorithms

1. Search algorithm: This algorithm takes a token to be searched as input and outputs the address of that token in the data structure / database.

2. Sorting algorithm: This algorithm is used to rearrange data structures based on a comparison operator, which decides the new order of data.

3. Greedy algorithm: This algorithm solves optimization problems by finding the locally optimal solution, assuming it's the optimal solution at the global level.

4. Hashing algorithm: This algorithm takes data and converts it into a uniform message with a hashing value.

5. Backtracking algorithm: This algorithm finds a solution to a given problem in incremental approaches and solves it one section at a time.

6. Divide and conquer algorithm: This algorithm divides a problem into many sub-problems. Each sub-problem can be solved independently to produce a sub-solution. These sub-solutions are then combined to make a solution.

7. Encryption algorithm: This algorithm takes data and maybe an encryption key as input and outputs an encrypted data. Concurrently a decryption algorithm takes the encrypted data and decryption key as input and outputs the original text.

8. Recursive algorithm: This algorithm calls itself repeatedly until a solution emerges. Recursive algorithms call themselves with a smaller values every time a recursive function is involved.

When writing algorithms to solve a problem in programming, or finding a solution to problem in general, we need to think about the following:

  1. What is the algorithm supposed to do?
  2. Does the algorithm do what it is supposed to do?
  3. Is the algorithm efficient? And can it be made more efficient?