# What is algorithm. Explanation with example.

September 26 2023

An algorithm is a step-by-step process or set of instructions for completing a certain activity or addressing a problem. It is a set of guidelines that can be followed to carry out a certain task or achieve a desired result. Natural language, pseudocode, mathematical notation, and programming languages are just a few of the different ways that algorithms can be expressed. They are employed in many different disciplines, including, but not limited to, computer science, mathematics, engineering, and operations research. For tasks like sorting a list of numbers, determining the shortest route between two points on a map, or developing a machine-learning model, algorithms can be basic or sophisticated.

### What makes a good algorithm?

The following traits should be included in a good algorithm:

1. Correctness: For each legitimate input, a good algorithm should provide the right result.

2. Clarity: The algorithm's steps should be obvious and simple to follow. Additionally, the algorithm must be simple to code.

3. Finiteness:  An effective algorithm must come to an end after a set number of steps.

4. Generality: A good algorithm should be general enough to work with a variety of inputs.

5. Efficiency: In terms of time and space complexity, a good algorithm should be efficient. This means that both the algorithm's execution time and memory consumption should be fair.

6. Simplicity: A good algorithm should have straightforward logic and structure that is simple and easy to grasp.

7. Flexibility: A strong algorithm should be adaptable to changing inputs or conditions.

8. Reusability: A good algorithm should be reusable, which means it may be adapted to other problems or used in new contexts.

### Examples

1. Algorithm to find the largest of three numbers:

1. Input: three numbers, say x, y and z.

2. Initialize a variable, say max, to the first number, x.

3. Compare the second number, y, with max. If y is greater than max, update max to be equal to y.

4. Compare the third number, z, with max. If z is greater than max, update max to be equal to z.

5. Output: max, which now contains the largest of the three numbers.

For example, if the input numbers are 5, 8, and 3, the algorithm will work as follows:

• max is initialized to 5
• y (8) is compared to max (5) and since 8 is greater than 5, max is updated to 8
• z (3) is compared to max (8) and since 3 is not greater than 8, max remains 8
• max (8) is returned as the largest number among the three input numbers.

This algorithm can be implemented in any programming language.

A different approach can be to find the maximum of x and y and then compare the result with z.

if x > y:
max = x
else:
max = y
if z > max:
max = z

This approach is also correct and will give the same result as the previous algorithm.

2. Algorithm to find odd number:

1. Input: an integer, n

2. Check if the remainder of n when divided by 2 is equal to 1.

3. If the remainder is equal to 1, return True (the number is odd)

4. If the remainder is not equal to 1, return False (the number is even)

This algorithm is quite simple, it checks if the number is odd by checking if the remainder of dividing the number by 2 is equal to 1. This can be implemented in any programming language.

3. Algorithm to add 3 numbers

1. Input: three numbers, say a, b, and c.

2. Initialize a variable, say sum, to 0.

3. Add a to sum: sum = sum + a

4. Add b to sum: sum = sum + b

5. Add c to sum: sum = sum + c

6. Output: sum, which now contains the sum of the three input numbers.

This algorithm is very simple and straightforward, it starts with initializing a variable sum to 0, then it adds the three input numbers to the sum one by one, the final value of sum will be the sum of the three numbers.

Note that you could also add the three numbers in one step, like sum = a + b + c, but the algorithm I provided is a step by step process of adding the numbers.