# Algorithms An algorithm is a step-by-step procedure that defines a set of instructions that must be executed in a specific order to produce the desired result. Algorithms are generally developed independently of underlying languages, which means that an algorithm can be implemented in more than one programming language.

The following are some important categories of algorithms in terms of data structure:

• Search – Algorithm for searching an item in a data structure.
• Sort – Algorithm for sorting items in a specific order.
• Insert – Algorithm for inserting a data structure item.
• Update – Algorithm for updating an existing item in a data structure.
• Delete – Algorithm for removing an existing item from a data structure.

Data structure is a method of organizing data so that it can be used efficiently. The following are the fundamental terms of a data structure. Each data structure has its own interface. The set of operations that a data structure supports is represented by the interface. An interface only provides a list of supported operations, the types of parameters they can accept, and the types of operations they can return.

Implementation A data structure’s internal representation is provided by implementation. The implementation also defines the algorithms used in the data structure’s operations.

Characteristics of Algorithm

The following characteristics should be present in an algorithm:

• The algorithm should be clear and unambiguous. Each of its steps (or phases), as well as their inputs and outputs, should be obvious and lead to only one meaning.
• Input -A well-defined algorithm should have 0 or more inputs.
• Output – An algorithm should have one or more well-defined outputs that correspond to the desired output.
• Algorithms must be terminated after a finite number of steps.
• With the available resources, feasibility should be possible.
• An algorithm should have step-by-step instructions that are independent of any programming code.

How to write an algorithm?

There are no well-defined algorithms writing standards. It is rather problem and resource dependent. Algorithms are never written to support a specific programming language.

As we all know, basic code constructs such as loops (do, for, while), flow control (if-else), and so on are shared by all programming languages. An algorithm can be written using these common constructs.

We usually write algorithms step by step, but this is not always the case. Algorithm writing is a process that begins after the problem domain has been defined. That is, we must understand the problem domain for which we are developing a solution.

Example Problem – Write an algorithm to add two numbers

Solution- Step 1 − START

Step 2 − declare three integers a, b & c

Step 3 − define values of a & b

Step 4 − add values of a & b

Step 5 − store output of step 4 to c

Step 6 − print c

Step 7 − STOP

Algorithm Analysis

The efficiency of an algorithm can be assessed at two stages: before and after implementation. They are as follows:

• A priori Analysis is a theoretical examination of an algorithm. An algorithm’s efficiency is measured by assuming that all other factors, such as processor speed, are constant and have no effect on the implementation.
• A Posterior Analysis is an empirical examination of an algorithm. The chosen algorithm is coded in a programming language. This is then run on the target computer machine. Actual statistics, such as running time and required space, are collected in this analysis.

Algorithm Complexity

Assume X is an algorithm and n is the size of the input data; the time and space used by the algorithm X are the two main factors that determine X’s efficiency.

• Time Factor – Time is measured by counting the number of key operations in the sorting algorithm, such as comparisons.
• Space Factor – The maximum memory space required by the algorithm is used to calculate the space factor.
• The complexity of an algorithm f(n) gives the algorithm’s running time and/or storage space in terms of n as the size of input data.

Space Complexity

The amount of memory space required by an algorithm during its life cycle is represented by its space complexity. The space required by an algorithm is equal to the sum of the two components listed below.

• A fixed part that is a space required to store certain data and variables that are independent of problem size. For example, simple variables and constants, programme size, and so on.
• A variable part is a space required by variables, the size of which is determined by the size of the problem. For instance, dynamic memory allocation, recursion stack space, and so on.

The complexity of space Any algorithm’s S(P) P is S(P) = C + SP(I), where C is the fixed part of the algorithm and S(I) is the variable part that depends on instance characteristic I. The following is a simple illustration of the concept.

Algorithm: SUM(A, B)

Step 1 –  START

Step 2 –  C ← A + B + 10

Step 3 –  Stop

Time Complexity

The time complexity of an algorithm represents the amount of time required to complete the algorithm. Time requirements can be defined as a numerical function T(n), where T(n) is the number of steps, provided each step takes the same amount of time.

For example, adding two n-bit integers requires n steps. As a result, the total computational time is T(n) = c n, where c is the time required to add two bits. T(n) grows linearly as the input size increases in this case.

0