1.
Introduction to Algorithmic
An algorithm is a sequence of well-
defined steps that solve a particular
problem.
Definition (Horowitz & Sahni)
• An algorithm must be finite, precise,
and efficient.
• It is expressed using pseudocode or
flowcharts.
Algorithm Efficiency
I
• Measured in terms of time
complexity and space complexity.
• The worst-case, best-case, and
average-case analysis is crucial.
2. Asymptotic Notations
These are mathematical tools to
describe an algorithm's running time.
Types
1. Big-0 Notation (0): Upper bound
(worst-case}
'f\n) = O(f(n))
Example: Binary search ➔ O(log n}
2. Omega (0): Lower bound (best-
case}
'I\ n) = (!(n))
Example: Bubble sort best case ➔ O(n}
3. Theta (0): Tight bound (average-
case}
'I\ n) = (!( n))
Example: Merge Sort ➔ 0(n log n}
3. Complexity - Time-Space
Tradeoff
An efficient algorithm balances time
(speed) and space (memory).
Example: Space-Time Tradeoff
• Using extra space for faster access
➔ Hash tables (use more space but
have 0(1) lookup time).
• Using less space but slower
execution
➔ Recursive algorithms (save space
but increase function call overhead).
I
4. Data Structures -
Definition and Classification
A data structure is a way of organizing
data to enable efficient operations.
Classification
1. Primitive Data Structures
• Integers, Floats, Characters,
Pointers
2. Linear Data Structures
• Arrays, Linked Lists, Stacks,
Queues
3. Non-Linear Data Structures
• Trees, Graphs, Hash Tables
4. Dynamic Data Structures
• Memory allocated dynamically
5.Arrays
An array is a collection of elements
stored in contiguous memory locations.
Memory Representation
• Stored in a single block of memory.
• Access elements using an index.
• Address formula:
; of A [i] = Base Address + (i x Size of each element)
Operations on Arrays
1 . Insertion
• At the end ➔ 0(1)
• At a specific position ➔ 0(n)
(shifting required)
2. Deletion
• From the end ➔ 0(1)
• From a specific index ➔ 0 (n)
3. Searching
• Linear Search ➔ 0(n)
• Binary Search ➔ 0(log n) (only for
sorted arrays)
4. Sorting
• Bubble Sort ➔ 0(n2 )
• Merge Sort ➔ 0(n log n)
6. Stacks and Queues
Stacks
A stack is a linear data structure that
follows LIFO {Last In, First Out).
Operations
1. Push ➔ Insert an element at the top.
2. Pop ➔ Remove the top element.
3. Peek ➔ Retrieve the top element.
Array Representation
#define MAX 100
int stack[MAX];
int top= -1;
void push(int x) {
if (top== MAX - 1) {
printf( 11 Stack Overflow");
return;
}
stack[++top] = x;
}
int pop() {
if (top== -1) {
printf( 11 Stack Underflow");
return -1;
}
return stack[top--];
}
Applications of Stacks
• Expression Evaluation (Postfix/
Infix)
• Backtracking (Maze Solving,
Recursive Calls)
• Function Calls (Call Stack in OS) I
Queues
A queue is a linear data structure that
follows FIFO (First In, First Out).
Operations
1. Enqueue ➔ Insert at the rear.
2. Dequeue ➔ Remove from the front.
Array Representation
#define tv1AX 100
int queue[MAX];
int front= -1, rear= -1;
void enqueue(int x) {
if (rear== MAX - 1) {
printf( 11 Queue Overflow");
return;
}
queue[++rear] = x;
if (front== -1) front= 0;
}
int dequeue() {
if (front== -1 I I front> rear) {
printf( 11 Queue Underflow");
return -1;
}
return queue[front++];
}
Applications of Queues
• Process Scheduling {OS)
• Handling Requests {Web Server,
Printer Queue)
• Graph BFS Traversal