# Completing Algorithms

## Completing Algorithms

**Understanding Algorithms**

- An
**algorithm**is a finite, well-defined procedure to solve a problem or perform a specific task. - Algorithms are important in computer science as they define a series of steps for a computer to follow in order to solve a problem or complete a task.
- Algorithms can be expressed in many forms, including natural language, pseudocode, flowcharts, and programming languages.

**Completing Algorithms**

- Completion of an algorithm involves determining the steps needed to solve a problem, and then implementing those steps in a structured format.
- The first step is problem understanding and decomposition. Analyze the problem and
**break it down**into manageable parts or sub-problems. This technique is known as**decomposition**. - Each sub-problem is then solved individually and the solutions are combined to solve the original problem - this is a technique called
**divided and conquer**.

**Designing Algorithms**

- When designing an algorithm, think about the
**inputs**needed and the**outputs**expected. - Also consider any
**data structures**(like arrays or lists) that might be used to hold data, or any**variables**that might need to be defined. - One of the most crucial considerations is the
**logic**of the algorithm – the sequence of operations (addition, subtraction, etc.), decision-making (if, else) and repetition (loops) that will ultimately solve the problem. - Always remember, a well-designed algorithm is efficient and doesn’t take unnecessary steps or process unnecessary data.

**Ensuring Algorithms are Correct**

- The correctness of an algorithm is extremely important - a correct algorithm accurately solves the problem it’s designed for.
- To ensure an algorithm’s correctness, use techniques such as
**dry running**, which involves tracing through an algorithm step by step to make sure it works as expected. - Frequently
**test**your algorithms with a variety of inputs (including edge cases) to ensure they produce the correct outputs and handle all scenarios. - Remember, an incorrect algorithm can cause serious problems, like incorrect results or infinite loops.

**Assessing Algorithm Efficiency**

- The efficiency of an algorithm refers to the amount of computational resources, namely time and space, it uses.
- Measure an algorithm’s time efficiency by considering its
**time complexity**- how the time to complete the algorithm increases as the size of the input increases. - Consider space efficiency by determining the algorithm’s
**space complexity**- how much memory is required to run the algorithm. - Always seek to optimise your algorithms to make them as efficient as possible.

**Adapting Algorithms into Code**

- Once an algorithm is completed, it becomes the blueprint to indicate the code development in a specific programming language.
- Be wary that different languages may require different constructs or syntax, but the algorithm dictates the
**core logic**. - Remember: the algorithm outlines the solution, and the code implements it.