Data Structures & Algorithms
CS - 2510
Spring 2025
Instructor’s Introduction
Syed Zaid Irshad
Senior Lecturer
Enrolled in PhD Computer Science at MAJU
9 Semester
681 Students
11 Programs
1148 Grades
15% Grade A/A-
11% Grade F/W
Couse Details
• Teams Code: 8blnr94
• Marks Breakup
• Midterm: 20 Marks
• Final Term: 50 Marks
• Assignment + Quizzes + Online Certificate: 30 Marks
• Course Outline is shared on Teams
Rules for this subject
• No Retake or Resubmission after deadline
• No correction of Attendance
• If you are late sit quietly or leave
• No cell phone usage except mine
• If you know about topic keep it to yourself unless I am saying
anything wrong
• Your behavior in class will determine how difficult Papers will be
• Do not sit in class without pen and paper
Topics for Midterm
• Intro to Data Structures
• Algorithm Analysis (Big O, Big Theta, Big Omega)
• Search Algorithms (Linear Search, Binary Search)
• Sorting Algorithms (Bubble Sort, Insertion Sort, Selection Sort,
Merge Sort, Quick Sort, Counting Sort)
• String Algorithms (Knuth-Morris-Pratt (KMP), Naive Pattern
Searching (NPS))
• Linked Lists (Single, Doubly, Circular, Search, Sort)
• Stacks (implementation using Array/LinkedList)
Introduction to Data Structures
• Definition:
A data structure is a way of organizing and storing data to perform
operations efficiently.
• Why are they important?
Helps manage large amounts of data
Improves efficiency in searching, sorting, and modifying data
Forms the foundation of programming and algorithms
Type of Data Structures
• Linear Data Structures:
• Array (Fixed-size collection of elements)
• Linked List (Dynamic memory allocation)
• Stack (LIFO - Last In, First Out)
• Queue (FIFO - First In, First Out)
• Non-Linear Data Structures:
• Tree (Hierarchical structure)
• Graph (Network-like connections)
• Hash Table (Key-Value mapping for fast access)
Array
Types of Array
• Static Array: A collection of elements stored in contiguous
memory locations.
• int arr[5] = {1, 2, 3, 4, 5};
• Dynamic Array: A Dynamic Array is an array that can grow or
shrink in size automatically during runtime.
• int* arr = new int[5];
• delete[] arr;
Dynamic Array vs. Static Array
Operation Static Array Dynamic Array
Memory Location Stack Heap
Resizable? NO Yes
Efficiency Fast Slower
Flexibility No Yes
Access arr[i] O(1) O(1)
Insertion at End Not Possible O(1)
Insertion at Middle O(n) O(n)
Delete at End Not Possible O(1)
Delete at Middle O(n) O(n)
Advantages & Disadvantages of Dynamic
Array
• Advantages:
Flexible size
Efficient memory allocation
Easier than linked lists (cache-friendly)
• Disadvantages:
Resizing can be costly (O(n) for copying elements)
Uses extra memory for resizing overhead
Linked List
• Definition: A collection of nodes, where each node contains
data and a reference to the next node.
• Types:
Singly Linked List
Doubly Linked List
Circular Linked List
Stack
• Definition: A data structure that follows Last In, First Out
(LIFO) order.
• Operations: Push (Insert), Pop (Remove), Peek (Top Element)
• Use Cases: Undo functionality, Backtracking algorithms
Queue
• Definition: A data structure that follows First In, First Out
(FIFO) order.
• Operations: Enqueue (Add), Dequeue (Remove), Peek (Front
Element)
• Use Cases: Task scheduling, Print queue
Trees
• Definition: A non-linear data structure with hierarchical
relationships.
• Types:
Binary Tree
Binary Search Tree (BST)
AVL Tree
B+ Tree
• Use Cases: File systems, Database indexing
Graph
• Definition: A collection of nodes (vertices) connected by
edges.
• Types:
Directed Graph
Undirected Graph
• Use Cases: Social networks, Google Maps
Algorithm Analysis
• Algorithm analysis is the process of evaluating the efficiency of
an algorithm in terms of time complexity and space complexity.
• It helps us determine the performance of an algorithm before
implementation.
Why Analyze Algorithms?
• Helps choose the most efficient algorithm for a problem.
• Predicts performance for large inputs (scalability).
• Avoids inefficient solutions that could lead to slow execution.
Time Complexity
Complexity Notation Example
Constant Time O(1) Accessing an element in an array arr[i]
Logarithmic Time O(log n) Binary Search
Linear Time O(n) Iterating through an array
Linearithmic Time O(n log n) Merge Sort, Quick Sort (average case)
Quadratic Time O(n²) Nested loops (e.g., Bubble Sort)
Cubic Time O(n³) Triple nested loops
Exponential Time O(2ⁿ) Recursion in Fibonacci
Factorial Time O(n!) Brute-force permutation generation
Space Complexity
Complexity Example
O(1) Using a few variables (constant space)
O(n) Storing an array of n elements
O(n²) Using a 2D matrix
O(n log n) Recursive merge sort
Best, Worst, and Average Cases
Case Meaning
Best Case Minimum time needed (ideal scenario)
Worst Case Maximum time needed (guaranteed upper bound)
Average Case Expected time for random inputs
Amortized Analysis
• Analyzes the average performance of an operation over multiple
runs.
Asymptotic Notations
• Asymptotic notations describe the growth rate of an algorithm's
running time or space usage as the input size n increases. They
help compare algorithms based on their efficiency.
• They allow us to ignore constant factors and focus on growth
trends.
• Help us determine whether an algorithm is scalable for large
inputs.
• Provide upper, lower, and tight bounds on algorithm performance.
Types of Asymptotic Notations
Notation Meaning Definition Use Case
Big-O (O) Upper Bound (Worst
Case)
T(n) ≤ c * f(n) for large n Ensures performance will not
exceed this limit
Big-Omega (Ω) Lower Bound (Best
Case)
T(n) ≥ c * f(n) for large n Guarantees the algorithm will at
least take this much time
Big-Theta (Θ)) Tight Bound
(Average/Exact Case
c₁ * f(n) ≤ T(n) ≤ c₂ * f(n)
for large n
Defines the exact growth rate
Little-o (o) Strict Upper Bound T(n) < c * f(n), but not
tight
Shows an algorithm grows strictly
slower than f(n)
Little-omega (ω) Strict Lower Bound T(n) > c * f(n), but not
tight
Shows an algorithm grows strictly
faster than f(n)
Searching Algorithm
• Searching algorithms are essential tools in computer science
used to locate specific items within a collection of data.
• These algorithms are designed to efficiently navigate through data
structures to find the desired information, making them
fundamental in various applications such as databases, web
search engines, and more.
Linear VS Binary
Linear Search Binary Search
Traverse each Element Divide data into half
Works on any sequence of data Only works if data is in proper sequence
Best Case: O(1), Worst / Average Case: O(n) Best Case: O(1), Worst / Average Case: O(log n)
Auxiliary Space: O(1) Auxiliary Space: O(1)
Deals with Small Datasets Deals with Large Datasets
Empty Array
Random Array
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Visualizing Linear Search
Code Snippet
Sorted Array
Visualizing Binary Search
Visualizing Binary Search
Visualizing Binary Search
Visualizing Binary Search
Visualizing Binary Search
Visualizing Binary Search
Visualizing Binary Search
Visualizing Binary Search
Code
Snippet
Trapping
Rainwater
Problem
Reshaping problem into Array
Left
Boundary
Right
Boundary
Reshaping problem into Array
Starting
Point
Reshaping problem into Array
M
H
L
M
H
R
Reshaping problem into Array
Left Max Right Max
Reshaping problem into Array
Min
Height
Starting
Point
=2-1=1
Reshaping problem into Array
Starting
Point
=2-1=1
Reshaping problem into Array
=2-1=1
M
H
L
M
H
R
Reshaping problem into Array
=2-1=1
Left/Right
Max
Reshaping problem into Array
=2-1=1
Min H/
Starting P
Reshaping problem into Array
=2-1=1
Starting
Point
Reshaping problem into Array
=2-1=1
M
H
L
M
H
R
Reshaping problem into Array
=2-1=1
Left Max Right Max
Reshaping problem into Array
=2-1=1
Min
Height
Starting
Point
=4-3=1
Reshaping problem into Array
=2-1=1
=4-3=1
Starting
Point
Reshaping problem into Array
=2-1=1
=4-3=1
M
H
L
M
H
R
Reshaping problem into Array
=2-1=1
Left Max Right Max
=4-3=1
Reshaping problem into Array
=2-1=1
Min
Height
Starting
Point
=4-3=1
=4-1=3
Reshaping problem into Array
=2-1=1
Starting
Point
=4-3=1
=4-1=3
Reshaping problem into Array
=2-1=1
=4-3=1
=4-1=3
M
H
L
M
H
R
Reshaping problem into Array
=2-1=1
Left Max Right Max
=4-3=1
=4-1=3
Reshaping problem into Array
=2-1=1
Min
Height
Starting
Point
=4-3=1
=4-1=3
=4-0=4
Total Trapped
Rainwater
• 1 + 1 + 3 + 4 = 9
• This is known as Naïve
Approach
• Time Complexity is O(n2)
• Space Complexity is O(1)
Sorting Algorithms
• Sorting refers to rearrangement of a given array or list of elements
according to a comparison operator on the elements.
• The comparison operator is used to decide the new order of
elements in the respective data structure.
Sorting Basics
• In-place Sorting: An in-place sorting algorithm uses constant space for producing the output
(modifies the given array only. Examples: Selection Sort, Bubble Sort, Insertion Sort and
Heap Sort.
• Internal Sorting: Internal Sorting is when all the data is placed in the main memory or internal
memory. In internal sorting, the problem cannot take input beyond allocated memory size.
• External Sorting : External Sorting is when all the data that needs to be sorted need not to be
placed in memory at a time, the sorting is called external sorting. External Sorting is used for
the massive amount of data. For example, Merge sort can be used in external sorting as the
whole array does not have to be present all the time in memory,
• Stable sorting: When two same items appear in the same order in sorted data as in the
original array called stable sort. Examples: Merge Sort, Insertion Sort, Bubble Sort.
• Hybrid Sorting: A sorting algorithm is called Hybrid if it uses more than one standard sorting
algorithms to sort the array. The idea is to take advantages of multiple sorting algorithms. For
example, IntroSort uses Insertions sort and Quick Sort.
Sorting Techniques
• There are various sorting algorithms are used in data structures.
The following two types of sorting algorithms can be broadly
classified:
o Comparison-based: We compare the elements in a comparison-based
sorting algorithm)
o Non-comparison-based: We do not compare the elements in a non-
comparison-based sorting algorithm)
Random Array
Bubble Sort
• Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements
if they are in the wrong order.
• Complexity Analysis of Bubble Sort:
o Time Complexity: O(n2)
o Auxiliary Space: O(1)
• Advantages of Bubble Sort:
o Bubble sort is easy to understand and implement.
o It does not require any additional memory space.
o It is a stable sorting algorithm, meaning that elements with the same key value maintain their relative order in the
sorted output.
• Disadvantages of Bubble Sort:
o Bubble sort has a time complexity of O(n2) which makes it very slow for large data sets.
o Bubble sort has almost no or limited real world applications. It is mostly used in academics to teach different ways of
sorting.
1st Pass (Pick & Compare)
1st Pass (Copy Bot 1 into Bot 3)
1st Pass (Copy Bot 2 into Bot 1)
1st Pass (Copy Bot 3 into Bot 2)
1st Pass (Pick & Compare)
1st Pass (Copy Bot 1 into Bot 3)
1st Pass (Copy Bot 2 into Bot 1)
1st Pass (Copy Bot 3 into Bot 2)
1st Pass (Pick & Compare)
1st Pass (Pick & Compare)
1st Pass (Copy Bot 1 into Bot 3)
1st Pass (Copy Bot 2 into Bot 1)
1st Pass (Copy Bot 3 into Bot 2)
1st Pass (Pick & Compare)
1st Pass (Copy Bot 1 into Bot 3)
1st Pass (Copy Bot 2 into Bot 1)
1st Pass (Copy Bot 3 into Bot 2)
1st Pass (Pick & Compare)