Unit 01: Programming Assignment Title: Algorithms and Programming Paradigms
Unit 01: Programming Assignment Title: Algorithms and Programming Paradigms
2
Example of Procedural Programming....................................................................................................21
Characteristic.....................................................................................................................................22
Pros and Cons of Procedural Programming.......................................................................................23
Object-Oriented Programming..............................................................................................................23
Pros and Cons of OOP........................................................................................................................23
Example of OOP.....................................................................................................................................24
Parallel Paradigm...............................................................................................................................24
Event –Driven Paradigm........................................................................................................................25
What is event-driven programming?.................................................................................................25
Characteristics of Event-Driven Programming.......................................................................................25
What makes an event-driven programming special?............................................................................26
Advantages of Event-Driven Programming........................................................................................27
Disadvantages of Event-Driven Programming...................................................................................27
Example of Event-Driven Paradigm.......................................................................................................27
Procedural VS Object-Oriented VS Event-Driven Paradigms.................................................................29
Conclusion.............................................................................................................................................29
3
List of Figures
Figure 1 Algorithm.......................................................................................................................................5
Figure 2 Flowchart to add two numbers......................................................................................................6
Figure 3 add two numbers entered by the users and print their sum.......................................................10
Figure 4 output of the algorithm...............................................................................................................10
Figure 5 Time and Space example.............................................................................................................12
Figure 6 Merge sort...................................................................................................................................15
Figure 7 Code for Fibonacci sequence using Dynamic Programming........................................................17
Figure 8 Output of Fibonacci sequence......................................................................................................17
Figure 9 Selection Sort...............................................................................................................................19
Figure 10 Selection Sort Output................................................................................................................19
Figure 11 Procedural Programming Paradigm...........................................................................................21
Figure 12 Output for Procedural Programming.........................................................................................22
Figure 13 OOP Code..................................................................................................................................24
Figure 14 OOP output................................................................................................................................24
Figure 15 Event-driven programming Code...............................................................................................28
Figure 16 Event-Driven Programming output............................................................................................28
List of Tables
Table 1 Algorithm VS Program.....................................................................................................................8
Table 2 Advantages and Disadvantages of Imperative Programming........................................................21
Table 3 Pros and Cons of OOP...................................................................................................................23
4
Section 1: Algorithms
What is Algorithm?
We have all seen food recipes, they usually contain a list of ingredients, along with set
of instructions to be followed to make the described meal similarly in computer science
an algorithm usually refers to a small procedure that solves a recurrent problem
(Gillis, 2022). In computer language, the recipe is referred as the procedure and
ingredients are known as inputs. Your computer looks at the procedure, follows it step
by step and you get to see desired outcomes, which are known as outputs.
Every time we use our phones, computers, laptops, or even calculators, we are using
algorithms. Programs work in a similar way. Their codes are made up of algorithm
telling them what and how to do something and give the desired outcome to the users
every single time.
Another example can be, let’s say you want to use a navigation app to get directions to
your favorite restaurant. So first you open the app and type in the address of the
restaurant, then the app uses different methods to look at various available directions to
reach that restaurant. Next, it uses different methods to check current traffic, then using
all this information it finds you the best available route to your favorite restaurant.
Both of the examples mentioned above, shows how a computer uses algorithm to
obtain result, which are better and faster in comparison to humans.
In simpler terms an algorithm is a step-by-step procedure or a set of rules followed by
your computer to solve a problem.
Figure 1 Algorithm
5
Algorithms can be written in pseudocode (a simplified programming language), in
English or any other language spoken by the programmer, programming language,
flowcharts and even control tables and yet the output will be the same. Natural
languages are rarely used as they are more ambiguous, whereas programming
languages, pseudocode, flowcharts and control tables are structured ways to express
algorithm that avoid ambiguities common in the statements based on natural language.
Programming languages are used for expressing algorithms executed by a computer.
Below, you can see a flowchart of an algorithm for adding two numbers entered by the
user (Anon., n.d.).
Instead of being all over the place algorithm has a start, a middle and an end, making it
look organized and easy for the users to understand. It also includes only what is
needed for the task to be completed, and excludes everything that is unclear to avoid
confusion.
6
Characteristics of Algorithm
Not all written instructions for programming is an algorithm. In order for the written
instructions to be algorithm, its needs to have or follow certain characteristic like:
Unambiguous and Clear: an algorithm must have specific, outlined steps. The steps
should be clear enough for the user to understand what to do at each step.
Well Ordered: the exact number of operations performed should be defined.
Feasibility: all steps should be possible.
Input and Output: should be defined precisely, along with being able to accept a well-
defined set of inputs and produce some result as an output.
Finiteness: should be able to terminate after a finite number of instructions.
Generality: being able to handle many inputs.
Language Independence: It should be language independent, meaning it can be
implemented in any language and still produce the same result.
Determinism: giving the same output for the same input case .
Also an algorithm can be analyzed by power consumption, CPU registers, network
data transfer rate, time and space.
Why do you need an Algorithm?
It is required for the following reason:
Scalability
It aids in your understanding, when you have a real-world problem, you can break it
down into smaller steps and analyze it, to solve the problem quickly.
Performance
It is challenging to break down real-world problem into smaller steps, so if the problem
can easily be divided into smaller steps it means it’s feasible.
Properties of Algorithm
It should terminate after a finite time.
It should produce at least one output.
It should be deterministic.
Every step must be effective.
It should take zero or more input.
7
How does algorithm work?
An initial input along with a set of instructions are used by algorithm, input can be in the
form of numbers or words, and these are needed to make decisions. The output is the
last step in an algorithm and is normally expressed as data.
For example, a search algorithm takes a search query as input and runs it through a set
of instructions for searching through a database for relevant items to the query.
Automation software acts as another example of algorithms, as automation follows a set
of rules to complete tasks. Many algorithms make up automation software, and they all
work to automate a given process (Gillis, 2022).
Algorithm vs. Program
Algorithm Program
An algorithm is a step-by-step procedure Set of various instructions that a computer
for computers to solve any given follows.
program.
Design Implementation
Domain Knowledge Programmer
Natural language, flowchart and many Program Language
more
Hardware and Software independent Hardware and software dependent
Analysis Testing
Table 1 Algorithm VS Program
Factors of an algorithm
The following are the factors to consider while designing an algorithm:
Functionality: it takes into account various logical steps to solve real-world problem.
Simplicity: It has to be simple, so it can easily be understood.
Extensibility: it should be extensible, so other designer or programmers can use if they
want to.
Robustness: ability of an algorithm to define your problem clearly.
Maintainability: it should be designed in a straightforward, structured way so you can
make changes in the future if needed.
Correctness: the algorithm should produce desired outcome when given an input,
meaning it is designed correctly.
Modularity: perfectly designed for the algorithm if you are given a problem and break it
down into small steps which is a basic definition of an algorithm.
8
How to design an algorithm?
In order to write an algorithm, there are some requirements:
1. Problem, that is to be solved by this algorithm.
2. Limitations, that are to be consider while solving the problem.
3. Input that is to be taken.
4. Output that are expected when the problem is solved.
5. Solution to this problem, in the given constraints.
With the help of the parameters mentioned above an algorithm is established that
solves the problem.
Example: Consider the example to add two numbers entered by the users and print
their sum.
STEP 1: Fulfilling the pre-requisites
As discussed above, in order to write algorithm, its requirements should be fulfilled.
a. Problem: add two numbers entered by the user and print their sum.
b. Limitations: the user can only enter integers any other data type is not
applicable.
c. Input: Two numbers to be added by the user.
d. Output: The sum of two numbers taken as input, in integer.
e. Solution to this problem, in the given constraints: The solution consist of adding
two numbers, it can be done by using’+’ operator or any method.
STEP 2: Designing the algorithm
With the help of the pre-requisites mentioned above, design the algorithm
Algorithm to add two numbers entered by the user and print the result:
1. START
2. Declare 2 integer variables, num1, num2
3. Enter 2 numbers to be added as input in variables num1 and num2.
4. Declare another integer variable where the resultant is to be stored, called sum.
5. Add num1 and num2 and assign the result to sum.
sumnum1+num2
6. Display and print the sum.
7. END
9
STEP 3: Testing and implementing the algorithm
Program:
Figure 3 add two numbers entered by the users and print their sum
Output:
10
space is required. It is dependent on the language of the complier and type of
hardware used.
11
Example:
12
Brute Force Algorithm
This the most common and simplest form of algorithm, its follows a straightforward
approach towards the problem, trying every possibility rather than advanced techniques
to improve efficiency.
13
Quick Sort: This algorithm picks a pivot element, rearranges the array elements in such
a way that all elements smaller than the picked pivot element move to left side of pivot,
and all greater elements move to right side. Finally, the algorithm recursively sorts the
sub arrays on left and right of pivot element.
Strassen’s Matrix multiplication: This technique is an efficient algorithm to multiply
two matrices.
The complexity of Divide and Conquer algorithm is calculated using the master
theorem which is as follow:
Example to find the time complexity of a recursive problem, for a merge sort the
equation can be written as:
T(n) = aT(n/b) + f(n)
= 2T(n/2) + 0(n)
a = 2 (each time, a problem is divided into 2 sub-problems)
n/b = n/2 (size of each sub problem is half of the input)
f(n) = time taken to divide the problem and merging the sub-problems
T(n/2) = O(n log n)
Therefore:
T(n) = 2T(n log n) + O(n) = O(n log n)
15
4. Stack overflow may cause the processing to fail, so make sure enough memory
is allocated to return stack.
Dynamic Algorithm
This the most sought out algorithm as it provides the most efficient way of solving the
problem. This type is also known as memorization technique, because instead of
calculating the result again and again it stores the previously calculated result. It goes
through 5 steps to find the best solution to the problem:
It divides the problem into sub-problems to find the best solution
After breaking down the problem, it finds the best solution from these sub-
problems.
Then it goes through the process of memorization, meaning sorting the result of
sub-problems.
Then reuse the result to prevent it from being recomputed for the same sub-
problem.
Finally, it computes the complex program’s output.
Dynamic algorithm has 2 properties:
Optimal Substructure: An optimal solution to the problem contain an optimal solution
to its sub-problems (Waraich, 2022).
Overlapping sub-problems: sub-problems are smaller variations of an original, larger
problem (Team, 2022).
This algorithm has 2 versions:
Bottom-up Approach: where the algorithm is solved from the bottom, meaning the last
possible sub-problem is solved first and using the result of this, the above sub-problems
are solved.
Top-Down Approach: where the algorithm is solved from the very beginning to arrive
at the required sub-problem and solve it using the previously solved sub-problems.
Problems that can be solved using this algorithm are:
Knapsack Problem, Longest Common Subsequence, Subnet Sum, Chain Matrix
Multiplication and etc.
The most common and simple example of this algorithm is Fibonacci sequence.
16
Code:
Output:
17
This algorithm is simple and easy on device. Common problems that can be solved by
this method are Huffman Coding, Prim’s Algorithm, Path Algorithm, Job
Scheduling, Selection sort and etc.
Example of Greedy Algorithm
Selection Sort, the best example, this sort works as follow:
Find the largest element exchange it with element in the last position.
Find the second largest element and exchange it with the second last position.
Continue in this way until the entire array is sorted.
5 23 10 20 2 33
5 23 10 2 20 33
5 2 10 20 23 33
5 2 10 20 23 33
2 5 10 20 23 33
2 5 10 20 23 33
Code:
18
Figure 9 Selection Sort
Output:
Complexity
Time complexity: O(n2)
Space Complexity: O(1)
19
What is Programming Paradigm?
Paradigm means pattern or a set of rules that define a standard. In context, of
programming the term paradigm means different ways or styles in which a program or
programming language can be organized, to create softwares.
One thing to remember is that programming paradigm does not specify the
programming language syntax, rather they simple define the program structure and the
set of principles that programming language complier should enforce.
Therefore, a programming paradigms is a framework that defines how a programmer
can model a complex problem to be solved, easily.
Each programming paradigm consist of different structure, features and opinions about
how a common problem can be solved. That is why different programming languages,
while being implemented follow different or combination of different strategies, and
these strategies are called paradigms. Like for example, C++ is a multi-paradigm
programming language that uses single or combination of approaches like Procedural
or Object-oriented programming, mixing in Generic or Functional programming
concepts.
There are many different types of programming paradigms that are used to fulfill each
and every demand. The paradigm can be broken down into 2 categories:
Declarative Programming Paradigms
This is a style of building program that expresses logic of computer without talking about
the control flow. It focuses more on what needs to be done instead of how it should be
done, and just declares the result.
The only difference between imperative and declarative programming is that imperative
is how to do and declarative is what to do.
Examples are logical, functional and database.
Imperative Programming Paradigms
Imperative is one of the oldest programming paradigm. It works by changing the
program state through assignment statements. It performs step by step task by
changing the state. It uses command to inspect and update variables, and store state in
program, then a procedure is created by the combinations of these command. Its main
focus is on how to achieve the goal.
Programming languages that use this include C, C++, Java, Python and etc.
Advantages Disadvantages
Very simple and easy to learn Optimization and extension is more
20
difficult
Efficient Higher risk of error when editing
Solution path is very easy for beginners to Codes can become very extensive and
understand thus confusing
Table 2 Advantages and Disadvantages of Imperative Programming
Output:
21
Figure 12 Output for Procedural Programming
Characteristic
Some key features of procedural programming language:
Predefined Functions
A set of predefined functions are available, these functions allow a programmer to
complete common task without having to create code themselves, and this is very time-
efficient.
Local Variables
Variable declared within the main structure of the method and is limited to local scope.
The local variables can only be used in the method it is defined in, and if used outside
the code will not work.
Global Variables
Unlike local variable, global variables increase functionality, they make themself
available to all method or functions in the code, providing the developer benefit of
accessing key data throughout the program.
Parameter Passing
This is a mechanism used to pass parameter to functions, subroutines or procedures to
complete computing actions on the data. The programmer places input parameters into
the module and receive output in return.
Modularity
This is a structure in which a developer divided the functionality of its code into a series
of small blocks. The programmer can then call these blocks, in their code to access
them at multiple points.
Top-down approach
This programming relies on top-down approach, under this approach a developer first
defines the primary goals then assess the component required to complete it.
22
Coding is easy and simple
Codes can be reused in several parts of the program
Consumes less memory on the computer
Program flow can be tracked easily
Cons
Focus is on function rather than data
Security is at risks, as the data is exposed to the world
Difficult to relate with real-world objects
Program codes are difficult to maintain and update because of global data
Object-Oriented Programming
The program is written as a collections of classes and object, which is based on
sending of messages to objects. These objects respond to the messages by performing
operations called methods.
Programming languages that uses this paradigm often handle complex application
better, and 3 most popular programming languages that use this are Java, C++ and
Python.
The paradigms core features are objects, classes, data encapsulation, inheritance
and polymorphism (use of a single symbol to represent multiple different types).
Classes are the foundational component of OOP, some classes inherit properties and
operations from other classes and this is often called a parent-child relationship by
developers.
Classes contain tools to hide and protect sensitive data through encapsulation, this is a
great advantage of this paradigm. Once the class is defined, we can use it to create
object.
Pros and Cons of OOP
Pros Cons
Codes can be used multiple time using Larger than other programs
class
Easy to maintain and modify Not suitable for some problems
Data Security Slower than other programs
Low-cost development Takes time to get used to
Data Redundancy Requires a lot of effort
Table 3 Pros and Cons of OOP
23
Example of OOP
Code:
Output:
Parallel Paradigm
Dividing the program instructions among multiple processors. The processing system of
this paradigm possess many different number of processor thus the programming time
is less. This approach is similar to divide and conquer, example of this paradigm are
NSL, C and C++.
24
Event –Driven Paradigm
For building complex system, event driven programming is a great approach. It includes
divide and conquer principle while allowing you to use other approaches like
synchronous calls.
What is event-driven programming?
In this paradigm, entities (objects, services and so on) communicate indirectly by
sending messages to one another through an intermediary. The messages are typically
stored in queue before being handled by the consumers.
Unlike direct calls, event-driven programming completely separate the producer from
the consumer, leading to some significant benefits. For example, multiple producer and
consumers can all collaborate to process incoming requests. Retrying failed operations
and maintain an event history are also simplified.
The flow of the program is determined by events, these are pieces of data that are sent
by producers and consumed by consumers. Events are user’s actions like mouse click
or key presses, sensor outputs, or message passing from other programs or by small
sequence of programmed instructions known as threads.
Key to event-driven paradigm is, producer and consumers are unaware of each other
and interact through message queues only.
Message queues are central location where messages are stored, either in memory or
durable storage. These are typically categorized based on topics, and consumers only
receive the queues which they are subscribed to, and this is the responsibility of the
message broker. Some popular ones are Redis, RabbitMQ and Apache Kafka.
This type of programming are dominant in graphical user interfaces and other
applications like JavaScript, where actions are based off on user inputs. Another system
that is event-driven is Node.j.
Generally, there is main loop in the event-driven applications that listens to events and
when an event is detected, a callback function is triggered. This program can be written
in any programming language, although languages that provide high-level abstractions
such as awaits and closures are preferred. Also, this programming makes it easier to
scale large systems, adding capacity simply by adding consumers.
Characteristics of Event-Driven Programming
Events
Events are actions that can be reacted to, like mouse click, keyboard and etc.
Event Handler
A type of function that run specific action when a specific event is triggered like for
example when clicking on a compose button, a tab to enter message is displayed or
when a send button is clicked, a “message sent” message is displayed.
25
Trigger Function
These are functions that decide on what code to run when a specific event occurs.
Service Oriented Processing
This is a key-feature in event-driven programming that are used to write programs, that
are made for services and they usually run in the background of OS. Also SOP doesn’t
slow down the computer but consumes little processing power of computer.
Time Driven
A code that runs on a time trigger, time driven can be a specific code that runs on
specific time, meaning it’s a pre-set to do task. Example window updates which a user
can set when to update.
What makes an event-driven programming special?
Tightly Coupled VS Loosely Coupled Workflow
Traditional call-based workflows are tightly coupled. Let’s look at an example, if person
A has some information Y, that is relevant to person B, then person A needs to follow
some criteria to deliver this information successfully to B, like:
a. It needs to know if B is interested in Y.
b. In case if something goes wrong, then it needs to know what to do
c. It also needs reference, needs to call to B and also know about its interface, and
a lot more.
But what if person C and D are also interested in information Y, but they have different
interfaces for receiving Y. For this A needs to get reference to C and D along with their
respective interfaces, it also needs to consider what to do in case of half failure, like if C
and D receive the information but B didn’t.
Whereas for loosely coupling, the only requirement that A needs to follow is send an
event Y to event queue. Now B, C and D can subscribe to event if they are interested.
Synchronous VS Asynchronous Calls
Garden variety function calls are known as Synchronous calls. A function with
arguments can be called and get the result in the same thread, blocking the threads
until function call returns.
Whereas Asynchronous calls return immediately without waiting for computation to be
complete. This is used by event-based system to receive events.
Request- response VS Publish- subscribe
Approach to communicate is typical of traditional web development is Request
response, for example searching something on search engine and receiving information
in respond by server.
26
Fire and forget are cases are tackled by publish subscribe approach, short for PubSub.
This simply publishes an event to queue without needing any responds.
Advantages of Event-Driven Programming
1. Flexibility: Event-driven programs can be altered easily if the programmer wants
to edit something, allowing them to produce a form for their requirement.
2. Suitability for Graphical Interface: This allows users to select different tools
like buttons, radio button and etc. also allowing them to create, put and edit the
object to their desired requirements
3. Simplicity of the Programming: You can directly edit the object you want to
code for, making it easier for user. Also the programming languages such as
visual basic usually have predictive coding, so it can predict codes from what the
user is typing and save time.
4. Good for models systems that need to be both asynchronous and reactive.
5. Simple and understandable
Disadvantages of Event-Driven Programming
1. Event-driven programs are often more complex than simple programs
2. Programs with complex GUI may be slower to load and run if RAM is insufficient.
3. Possible tight coupling between event and consumer
4. Difficult to find error
5. The flow of the program is less obvious and logical
Example of Event-Driven Paradigm
Code and Output:
27
Figure 15 Event-driven programming Code
28
Procedural VS Object-Oriented VS Event-Driven Paradigms
Procedural Object-Oriented Event-Driven
Character user interface is Commands written in To create the program GUI
provided to write the modules are provided. is provided.
command.
Commands are written and Specific task are Actions are defined on
executed in linear fashion. performed by interacting events, like mouse click,
with object and class. keyboard.
Conclusion
Due to the increase need for large and complicated programs, programming paradigms
have been evolving continuously. In today’s world OOP and EDP are the most preferred
paradigm in comparison to others. These paradigm not only provide advantages like
reusability of codes, but also isolate the user from the internal working of the program
along with accepting many different and complex instructions, leading to decline of
procedural programming.
29
References
Anon., n.d. Flowchart In Programming. [Online]
Available at: https://www.programiz.com/article/flowchart-programming
Team, I. E., 2022. Dynamic Programming: Characteristics, Methods and Examples. [Online]
Available at: https://www.indeed.com/career-advice/career-development/dynamic-programming
Upadhyay, S., 2022. What Is An Algorithm? Characteristics, Types and How to write it. [Online]
Available at: https://www.simplilearn.com/tutorials/data-structure-tutorial/what-is-an-
algorithm#why_do_you_need_an_algorithm
30