Data Structures and Algorithms: Class Notes
Introduction
These notes cover fundamental concepts in Data Structures and Algorithms, essential for computer
science students. Topics include linked lists, time complexity, and sorting algorithms, with examples
and exercises.
1. Linked Lists
A linked list is a linear data structure where elements are stored in nodes. Each node contains data and
a reference to the next node.
1.1 Structure
A node in a singly linked list can be represented as:
class Node:
def __init__(self, data):
self.data = data
self.next = None
1.2 Diagram
Text-based representation of a linked list (Head -> Node1 -> Node2 -> None):
[Head] --> [Node1 | data: 5 | next] --> [Node2 | data: 10 | next] --> [None]
2. Time Complexity
Time complexity measures the efficiency of an algorithm as the input size grows. Common notations
include O(1), O(n), O(n²).
2.1 Common Complexities
Operation Complexity Description
Constant O(1) Fixed time regardless of input size
Linear O(n) Time grows linearly with input size
Quadratic O(n²) Time grows quadratically, e.g., nested loops
3. Bubble Sort Algorithm
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order.
3.1 Pseudocode
procedure bubbleSort(arr):
n = length(arr) --
for i from 0 to n-1:
for j from 0 to n-i-1:
if arr[j] > arr[j+1]:
swap(arr[j], arr[j+1])
Time Complexity: O(n²)
4. Exercises for Students
1. Implement a singly linked list with insert and delete operations. 2. Analyze the time complexity of
searching in a linked list vs. an array. 3. Modify bubble sort to terminate early if the list is sorted.