Lecture No. 2 Algorithm
Lecture No. 2 Algorithm
2
Data Structures &
Algorithms
LECTURER ABDUL MUNIM KHAN
Algorithms
Algorithms are step-by-step procedures for solving problems.
In computer science, algorithms are used to process data stored in data
structures.
Some common algorithms include sorting algorithms (e.g., bubble sort,
quick sort, etc.), search algorithms (e.g., linear search, binary search,
etc.), and graph algorithms (e.g., breadth-first search, depth-first search,
etc.).
The efficiency of an algorithm depends on the time and space it takes to
complete its task. This is measured in terms of time complexity and space
complexity, respectively.
Why Learn Algorithms?
•To solve complex problems systematically.
•To optimize performance in terms of time and resources.
•Forms the backbone of fields like:
• Software Development
• Data Science
• Artificial Intelligence
• Cybersecurity
Examples of Algorithms
1.Real-life Example:
1.Recipe for cooking a dish.
2.Computer Science Example:
1.Sorting a list of numbers.
2.Finding the shortest path in a graph.
Algorithmic Notations
Algorithmic notations are symbolic or textual representations used to
describe algorithms in a structured and precise manner.
These notations help convey the logic, steps, and flow of an algorithm,
making it easier to understand, analyze, and implement.
These are:
1. Natural Language Description
2. Pseudocode
3. Flowchart
4. Mathematical Notations
5. Programming Code
Control Structures
Control structures are fundamental building blocks in
algorithms and programming that dictate the flow of
execution.
They determine the order in which statements are
executed, allowing algorithms to make decisions, repeat
actions, and handle different scenarios dynamically.
Understanding control structures is essential for designing
efficient and effective algorithms.
1. Sequence Logic
(Sequential Flow)
•The sequence logic represents a straightforward, linear execution of
instructions. Statements are executed one after the other in the
order they appear.
Key Features:
•Linear Flow: Executes statements sequentially without any
branching or looping.
•Simplicity: The most basic control structure, forming the foundation
for more complex structures.
Example
2. Selection (Decision-
Making)
•Selection structures allow algorithms to choose different paths
based on certain conditions. This enables decision-making within the
algorithm.
Key Types:
1.Single Alternative (If Statement)
2.Double Alternative (If-Else Statement)
3. Multiple Alternative (Else-If Ladder)
Single Alternative (If
Statement)
Description:
•Executes a block of code only if a specified condition is true.
Double Alternative (IF-Else)
Executes one block of code if a condition is true and another block if it's false.
Multiple Alternative (Else-If
Ladder)
Allows multiple conditions to be checked sequentially, executing the corresponding block for
the first true condition.
3. Iteration Logic (Repetitive
Flow) Loops
•Iteration structures allow algorithms to repeat a block of code multiple times based on certain
conditions. This is essential for tasks that require repetition, such as processing items in a list or
performing calculations until a condition is met.
Key Types:
1.For Loop
2.While Loop
3.Do-While Loop
4.Foreach Loop (in some languages)
Repeat For Structure
Repeat While
•Repeats a block of code as long as a specified condition is true. Suitable when
the number of iterations is not known in advance.