Python Data Structure Algorithm Design
Python Data Structures: Algorithm Design
An algorithm is a step-by-step procedure that defines a set of instructions to be executed in a certain order to obtain a desired output. Algorithms are often created independently of the underlying programming language, meaning that an algorithm can be implemented in more than one programming language.
From a data structure perspective, the following are some important categories of algorithms:
- Search – Algorithms that search for an item in a data structure.
-
Sorting – Algorithms that sort items into a certain order.
-
Insert – Algorithms that insert items into a data structure.
-
Update – Algorithms that update an existing item in a data structure.
-
Delete – An algorithm that deletes an existing item from a data structure.
Characteristics of an Algorithm
Not all programs qualify as algorithms. An algorithm should have the following characteristics.
- Unambiguous – An algorithm should be clear and unambiguous. Each of its steps (or phases), as well as their inputs/outputs, should be clear and must lead to only one meaning.
-
Input – An algorithm should have zero or more well-defined inputs.
-
Output – An algorithm should have one or more well-defined outputs that should match the expected output.
-
Finiteness – An algorithm must terminate after a finite number of steps.
-
Feasibility – It should be feasible given the available resources.
-
Independence – An algorithm should have step-by-step instructions and should be independent of any programming code.
How to Write an Algorithm
There is no clear standard for writing an algorithm. Instead, it depends on the problem and the resources. Algorithms are never written to support a specific programming code.
As we know, all programming languages share basic code structures, such as loops (do, for, while) and control flow (if-else). These common structures can be used to write an algorithm.
We write algorithms in a step-by-step manner, but this is not always the case. Algorithm writing is a process that is performed after the problem domain is defined. In other words, we should understand the problem domain and design a solution for it.
Example
Let’s learn how to write an algorithm using an example.
- Problem – Design an algorithm to add two numbers and display the result.
Step 1 – Start
Step 2 – Declare three integers a, b, and c
Step 3 – Define the values of a and b
Step 4 – Increment the values of a and b
Step 5 – Store the output of Step 4 into c
Step 6 – Print c
Step 7 – Stop
An algorithm tells a programmer how to code a program. Alternatively, the algorithm can be written as follows:
Step 1 – Start ADD
Step 2 – Get the values of a and b
Step 3 – c ← a + b
Step 4 – Display c
Step 5 – Stop
In algorithm design and analysis, the second method is often used to describe an algorithm. It makes it easy for the analyst to analyze the algorithm, ignoring any unnecessary definitions. The analyst can observe which operations are being used and how the process flows.
Writing the step numbers is optional.
We design an algorithm to obtain a solution to a given problem. A problem can be solved in multiple ways.
Thus, for a given problem, many solutions can be derived. The next step is to analyze these proposed solutions and implement the most suitable one.