Algorithmic - Advanced Programming in Python
1st Year Master SYS-TELECOM/AUT&SYS/ELN-INST/HSE
Departement of Electrical Engineering/HSE
Faculty of Technology & Institut des Sciences et Techniques Appliquées (ISTA)
University of Skikda
October 22, 2025
1 Introduction to Algorithms
Algorithms play a vital role in the fields of computer science and programming as they
help to solve and implement complex technological and computing problems efficiently.
An algorithm is also used to make the implementations of the problems clear, easy to
repair, simple to update and upgrade.
Definition 1. generally speaking, an algorithm is an abstraction of the implementation of
a computing problem in the form of pseudo-code or block diagrams. It describes the set
of sequential/parallel rules or instructions that leads to achieve, at the end, the desired
results, or outputs based on the data provided as inputs at the beginning.
Examples of algorithms given in the forms of pseudo-code and in block diagram are
presented in the following figures:
M1 - Algorithmic - Advanced Programming in Python - Oct. 22 2025 [Link]@[Link] 1/6
An algorithm written in pseudo-code An algorithm described with a block
diagram (www.c-programming-
[Link])
To fulfill the function of an algorithm, this latter must be written or designed with the
following in mind:
1. Clearly highlight the entry (i.e., start or begin) and exit (i.e., end or finish) points of
the algorithm. In the usual convention, we use terms like Start, Begin and Entry to
label the entry point. We also use terms like Finish, End, and Exit to design the exit
point of an algorithm. For a block diagram based algorithm, we adopt the following
block symbols to refer to entry and exit points:
The oval-shaped geo- The oval-shaped ge-
metric object is used to ometric object is also
denote the entry point used to design the exit
of an algorithm. point of an algorithm.
2. When designing an algorithm, inputs and outputs should be referred to by terms
such as Input and Output or Return Output. For block diagrams, we usually use a
parallelogram-shaped object to design the inputs and outputs:
M1 - Algorithmic - Advanced Programming in Python - Oct. 22 2025 [Link]@[Link] 2/6
Inputs Outputs
3. Instructions or operations are written explicitly in an algorithm and placed inside
rectangles in case of block diagrams as follows:
Examples of rectangles contain-
ing instructions
4. In case of algorithms requiring conditions, words like if, then, else, ifelse, and endif
are used in pseudo code. On the other hand, rhombus shaped objects are used in
block diagrams as follows:
An example of a rhombus containing a con-
dition
Example 1. an example of a concrete application of an algorithm to be established is the
problem of sorting a sequence of n numbers (𝑎0 , 𝑎1 , ⋯ , 𝑎𝑛−1 ). Here we can define the
input and output as:
Input: (𝑎0 , 𝑎1 , ⋯ , 𝑎𝑛−1 )
′ ′ ′ ′ ′ ′
Output: (𝑎0 , 𝑎1 , ⋯ , 𝑎𝑛−1 ) where (𝑎0 ≤ 𝑎1 ≤ ⋯ ≤ 𝑎𝑛−1 ).
M1 - Algorithmic - Advanced Programming in Python - Oct. 22 2025 [Link]@[Link] 3/6
As an example for the input sequence: (6, 1, 9, 4, 7), the output should be the following
sorted sequence: (1, 4, 6, 7, 9).
1.1 Algorithm efficiency
For the same problem, different algorithms can achieve the same desired solution. How-
ever, they differ in the execution time required to obtain the result. This execution time is
the main criterion for evaluating the efficiency of an algorithm. For example, the sorting
problem mentioned above can be solved by two different algorithms. The first is called
insertion sort, which takes time roughly equal to 𝛼 × 𝑛2 to sort 𝑛 elements, where 𝛼 is a
constant that does not depend on 𝑛. Whereas the second algorithm, called merge sort,
consumes 𝛽×𝑛×𝑙𝑜𝑔2 (𝑛), where 𝛽 is a constant that also does not depend on 𝑛. In general,
𝛼 < 𝛽. In general, constant factors 𝛼 and 𝛽 have much less impact on running time than
dependence on the size of the input 𝑛.
It is easy to see that insertion sort is often faster than merge sort for small input sizes.
Once the input size 𝑛 becomes large enough, the advantage of merge sort is 𝑙𝑜𝑔2 (𝑛) over
𝑛.
𝑙𝑜𝑔(1000)
As an application, to sort 𝑛 elements, when 𝑛 = 1000, so 𝑙𝑜𝑔2 (1000) = 𝑙𝑜𝑔(2) =
3
𝑙𝑜𝑔(2)
= 9.9657 ≈ 10. Furthermore, for 𝑛 = 1000000 we obtain 𝑙𝑜𝑔2 (𝑛) ≈ 20.
1.2 Support software
In order to write interesting algorithm pseudocode, several Latex packages are available.
These include algorithm, algorithmx, algpseudocode, and algorithm2e. For block diagram-
based algorithms, there is Vue ([Link] which is free but requires
Java version 11 to run and is available for all platforms Windows, MacOS, and Linux. An-
other useful software called Xmind which has a limited free version but sufficient for
algorithm design. It is available for Windows, MacOS and Linux at the following link:
[Link]
2 Labwork
Exercise1 Sorting Algorithm: Getting started with indexed arrays and loops
We would like to write and design an algorithm that sorts or rearranges a sequence
of items, such as numbers, in ascending order.
To do this, let us define
(𝑎𝑖 )𝑖=𝑛−1
𝑖=0 , (1)
M1 - Algorithmic - Advanced Programming in Python - Oct. 22 2025 [Link]@[Link] 4/6
as a sequence of 𝑛 numbers (𝑎0 , 𝑎1 , ⋯ , 𝑎𝑛−1 ) where 𝑛, 𝑖 ∈ ℕ.
The well-suited result of our sorting algorithm is to obtain a new output sequence
(𝑏𝑖 )𝑖=𝑛−1
𝑖=0 , (2)
in which
{𝑎𝑖 }𝑖=𝑛−1
𝑖=0 = {𝑏𝑖 }𝑖=𝑛−1
𝑖=0 , (3)
such as (𝑏0 ≤ 𝑏1 ≤ ⋯ ≤ 𝑏𝑛−1 ).
For example, if we assume that (𝑎𝑖 ) = (51, 12, 39, 77, 1), (𝑏𝑖 ) should be equal to (1, 12, 39, 51, 77).
1. How is sorting problem solving useful in real life? Give some examples?
2. Highlight the input and output of your sorting algorithm?
3. Write a pseudo-code of an algorithm that solves the sorting problem described
above?
4. Design an algorithm based on a block diagram, similar to a flowchart, that solves
the sorting problem given above? (you must take into consideration the instructions
you have seen in the course).
5. In order to validate your proposed sorting algorithm, you need to translate it into a
program (i.e., implement).
(a) Write a Matlab script that implements your sorting algorithm? (application:
(𝑎𝑖 ) = (99, 32, 8, 3, 100)).
(b) Write a C program that implements your sorting algorithm? (application: (𝑎𝑖 ) =
(99, 32, 8, 3, 100))
6. Calculate the complexity of your suggested algorithm based on the number of ele-
ments 𝑛?
Exercise2 K-Nearest Neighbor Algorithm: Efficiency & Complexity
The k-Nearest Neighbors (k-nn) algorithm ranks an item based on the number of near-
est items. An example of problems that may involve k-nn algorithms is illustrated in the
following figure:
M1 - Algorithmic - Advanced Programming in Python - Oct. 22 2025 [Link]@[Link] 5/6
An example of a classification problem
that may involve the k-nn algorithm.
In this example, the point named ”new data point” is assigned to Category 2 (i.e., blue
rhombuses or diamonds). This is because the number of the most of its nearest neighbors
of the Category 2, which is 3, is greater then the number 2 of those of Category 1 (i.e., red
rhombuses). This example uses a k-nn algorithm with 𝑘 = 5.
Formulation 1. let 𝐶1 and 𝐶2 be two classes of 2D points in which each point is defined
by its coordinate system (𝑥, 𝑦). We wish, for a new point 𝑞, to find which of the classes 𝐶1
and 𝐶2 is most likely to represent 𝑞 based on the Euclidean distance to each point in the
class. This is achieved using the k-nn algorithm.
1. Find some applications of this problem in our real life that requires the use of k-nn
type algorithms?
2. Define the input and output parameters of your version of the k-nn algorithm?
3. Design your own k-nn algorithm that leads to classifying a new invisible point 𝑞(𝑥, 𝑦)
in the class 𝐶1 or the class 𝐶2 ?
4. Implement your suggested k-nn algorithm in Matlab and C?
5. How fast is your k-nn algorithm compared to the one implemented in Matlab as a
built-in function: knnsearch()?
M1 - Algorithmic - Advanced Programming in Python - Oct. 22 2025 [Link]@[Link] 6/6