2introduction To Data Structure Algorithms
2introduction To Data Structure Algorithms
CPE-PC 215
INTRODUCTION TO DATA
STRUCTURE & ALGORITHMS
SECOND YEAR
ALGORITHM
ALGORITHM
An algorithm is a Step By Step process to solve a problem,
where each step indicates an intermediate task. Algorithm
contains finite number of steps that leads to the solution of
the problem
Properties /Characteristics of an
Algorithm
Input-Output: Algorithm takes ‘0’ or more input and produces the
required output. This is the basic characteristic of an algorithm.
Finiteness: An algorithm must terminate in countable number of
steps.
Definiteness: Each step of an algorithm must be stated clearly
and unambiguously.
Effectiveness: Each and every step in an algorithm can be
converted in to programming language statement.
Generality: Algorithm is generalized one. It works on all set of
inputs and provides the required output. In other words it is not
restricted to a single input value.
CATEGORIES OF
ALGORITHM
Based on the different types of steps in an Algorithm, it can be divided into three categories, namely
Sequence
Selection
Iteration
SEQUENCE
The steps described in an algorithm are performed
successively one by one without skipping any step. The
sequence of steps defined in an algorithm should be simple
and easy to understand. Each instruction of such an
algorithm is executed, because no selection procedure or
conditional branching exists in a sequence algorithm.
SEQUENCE
EXAMPLE:
// adding two numbers
Step 1: start
Step 2: read a,b
Step 3: Sum=a+b
Step 4: write Sum
Step 5: stop
SELECTION
The sequence type of algorithms are not
sufficient to solve the problems, which
involves decision and conditions. In order to
solve the problem which involve decision
making or option selection, we go for
Selection type of algorithm.
SELECTION
The general format of Selection type of statement is as
shown below:
if(condition)
Statement-1;
else
Statement-2;
The above syntax specifies that if the condition is true, statement-1 will be executed otherwise
statement-2 will be executed. In case the operation is unsuccessful. Then sequence of algorithm
should be changed/ corrected in such a way that the system will reexecute until the operation is
successful
SELECTION
SELECTION
ITERATION
Iteration type algorithms are used in solving
the problems which involves repetition of
statement. In this type of algorithms, a
particular number of statements are repeated
‘n’ no. of times.
ITERATION
Example 1
Step 1 : start
Step 2 : read n
Step 3 : repeat step 4 until n>0
Step 4 : (a) r=n mod 10
(b) s=s+r
(c) n=n/10
Step 5 : write s
Step 6 : stop
Performance Analysis an Algorithm:
The Efficiency of an Algorithm can be measured by the
following metrics.
1. Time Complexity
2. Space Complexity.
TIME COMPLEXITY
The amount of time required for an algorithm
to complete its execution is its time
complexity. An algorithm is said to be
efficient if it takes the minimum (reasonable)
amount of time to complete its execution.
SPACE COMPLEXITY
The amount of space occupied by an
algorithm is known as Space Complexity. An
algorithm is said to be efficient if it occupies
less space and required the minimum amount
of time to complete its execution.
ACTIVIT
1. Write an algorithm to find the factorial of a number entered by
the user.
Y
2. Write an algorithm to find the Simple Interest for a given principal
amount, time, and rate of interest.
3. Write an algorithm to add two numbers.
4. Write an algorithm to check if a person is eligible to vote.
5. Write an algorithm to calculate the average of a list of numbers.
6. Write an algorithm to find the sum of the digits of a given
number.
STACKS & HEAP
MEMORY
STACKS
- The Stack is a region of memory used to
manage function calls and local variables. It's a
Last-In-First-Out (LIFO) structure, which makes
it fast but limited in size.
STACKS
- A place in the computer memory where all
variables that are declared and initialized
before runtime are stored.
- A temporary storage memory, if you come out
of the program the memory of the variable will
not be there.
- Any data on the stack for a function will
automatically be deleted.
STACKS
- Data is added or removed in Last-in-first-out
manner (LIFO)
STACKS
- The stack has a fixed size
- Both stack and heap are stored on the RAM
- If there is not enough space on the stack to
handle the memory being assigned to it, a
stack overflow occurs.
STACKS
Key Characteristics of Stack
- Automatic memory management: When a function is called,
local variables are automatically pushed onto the stack. When
the function exits, the memory is automatically reclaimed.
Key Characteristics of Stack
- Fixed size: The stack has a limited size, and if it exceeds the
available memory, it leads to a stack overflow.
EXAMPLE
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
EXAMPLE
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
- Binary Trees: Nodes in trees (e.g., binary search trees, AVL trees,
heaps) are dynamically allocated on the heap to allow insertion and
deletion of elements during runtime.
USE IN DATA STRUCTURES
Dynamic Arrays: Unlike static arrays (which are allocated on the
stack), dynamic arrays (like ArrayList in Java or std::vector in C++)
allocate memory from the heap to allow resizing.
EXAMPLE
struct Node {
int data;
Queue:
- Operations: enqueue(), dequeue(), front(), isEmpty()
- Behavior: Follows the First-In-First-Out (FIFO)
Common ADTs
Graph:
- Operations: addVertex(), addEdge(), removeVertex(), traverse()
- Behavior: A collection of vertices connected by edges, representing
relationships between objects.
Set:
- Operations: add(), remove(), contains(), union(), intersection()
- Behavior: A collection of distinct elements with no particular order.
int main() {
Stack s; // Declare a Stack 's' (we assume 'Stack' is a predefined struct)
initStack(&s); // Initialize the stack
printf("Top element: %d\n", peek(&s)); // Peek at the top element of the stack
printf("Popped: %d\n", pop(&s)); // Pop the top element (30) and print it
printf("Popped: %d\n", pop(&s)); // Pop the next top element (20) and print it
printf("Is stack empty? %d\n", isEmpty(&s)); // Check if the stack is empty (returns 0 for false or 1 for true)