Unit 3: Sorting & Graphs



Introduction to Sorting

Sorting is the process of arranging data elements in a specific order, usually ascending or descending.
Sorting improves:

  • Searching efficiency
  • Data presentation
  • Duplicate detection
  • Database operations

Insertion Sort

Insertion Sort works the same way we arrange playing cards in our hand.
It builds the sorted list one element at a time by inserting each element at its correct position.

Algorithm (Steps)

  1. Assume the first element is sorted.
  2. Pick the next element.
  3. Compare it with elements on the left.
  4. Shift larger elements one position right.
  5. Insert the element at the correct position.
  6. Repeat until the list is sorted.

Example

Input: 8 3 5 2
Passes:

  • 8 | 3 → 3 8

  • 3 8 | 5 → 3 5 8

  • 3 5 8 | 2 → 2 3 5 8

Time Complexity

CaseComplexity
BestO(n)
AverageO(n²)
WorstO(n²)

Space Complexity

  • O(1) (In-place)

Advantages

  • Simple and easy to implement
  • Efficient for small data sets
  • Stable sorting algorithm

Disadvantages

  • Very slow for large datasets

Applications

  • Small lists
  • Nearly sorted data

Selection Sort

Selection Sort repeatedly selects the smallest element from the unsorted part and places it at the beginning.

Algorithm

  1. Find the minimum element.
  2. Swap it with the first position.
  3. Repeat for remaining elements.

Example

Input: 64 25 12 22 11

Passes:

  • Select 11 → 11 25 12 22 64
  • Select 12 → 11 12 25 22 64
  • Select 22 → 11 12 22 25 64

Time Complexity

CaseComplexity
BestO(n²)
AverageO(n²)
WorstO(n²)

Space Complexity

  • O(1)

Advantages

  • Simple algorithm
  • Minimum number of swaps

Disadvantages

  • Always slow (O(n²))
  • Not stable

Applications

  • Useful when memory writes are costly

Bubble Sort

Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order.

Algorithm

  1. Compare adjacent elements.
  2. Swap if needed.
  3. Largest element “bubbles” to the end.
  4. Repeat until no swaps are needed.

Example

Input: 5 1 4 2 8

Passes:

  • 1 4 2 5 8
  • 1 2 4 5 8

Time Complexity

CaseComplexity
BestO(n)
AverageO(n²)
WorstO(n²)

Space Complexity

  • O(1)

Advantages

  • Very easy to understand
  • Stable algorithm

Disadvantages

  • Extremely inefficient for large lists

Applications

  • Educational purposes only

Heap Sort

Heap Sort uses a Binary Heap data structure to sort elements.

Working Principle

  1. Build a Max Heap.
  2. Remove the root (largest element).
  3. Place it at the end.
  4. Repeat for remaining elements.

Example

Input: 4 10 3 5 1

  • Build Max Heap
  • Extract max repeatedly → Sorted array

Time Complexity

CaseComplexity
BestO(n log n)
AverageO(n log n)
WorstO(n log n)

Space Complexity

  • O(1)

Advantages

  • Fast and efficient
  • No extra memory required

Disadvantages

  • Not stable
  • Complex implementation

Applications

  • Priority queues
  • Large datasets

Comparison of Sorting Algorithms

AlgorithmBestAverageWorstStableIn-Place
BubbleO(n)O(n²)O(n²)YesYes
SelectionO(n²)O(n²)O(n²)NoYes
InsertionO(n)O(n²)O(n²)YesYes
HeapO(n log n)O(n log n)O(n log n)NoYes

Sorting in Linear Time

Counting Sort

Counting Sort works by counting occurrences of elements instead of comparisons.

Algorithm

  1. Find the maximum value.
  2. Create a count array.
  3. Store frequency of each element.
  4. Compute cumulative sum.
  5. Place elements in sorted order.

Time Complexity

  • O(n + k)

Space Complexity

  • O(k)

Advantages

  • Very fast for small range values
  • Stable

Disadvantages

  • Not suitable for large values

Applications

  • Sorting marks, ages, IDs

Bucket Sort

Bucket Sort divides elements into multiple buckets, sorts them individually, and merges them.

Algorithm

  1. Create empty buckets.
  2. Distribute elements into buckets.
  3. Sort each bucket.
  4. Merge all buckets.

Time Complexity

  • Average: O(n)
  • Worst: O(n²)

Space Complexity

  • O(n)

Advantages

  • Very fast for uniformly distributed data

Disadvantages

  • Requires extra memory
  • Not suitable for non-uniform data

Applications

  • Floating-point numbers
  • Large datasets with uniform distribution

Conclusion (Exam Ready)

  • Insertion, Bubble, Selection → Simple but slow
  • Heap Sort → Efficient and widely used
  • Counting & Bucket Sort → Linear time but data-specific

GRAPHS 

A graph is a non-linear data structure used to represent relationships between objects.

A graph G is defined as:

G=(V,E)G = (V, E)

Where:

  • V = Set of vertices (nodes)
  • E = Set of edges (connections)

Terminology Used with Graph

TermDescription
Vertex (Node)Basic unit of graph
EdgeConnection between two vertices
Directed GraphEdges have direction
Undirected GraphEdges have no direction
Weighted GraphEdges have weights
DegreeNumber of edges connected to a vertex
In-DegreeNumber of incoming edges
Out-DegreeNumber of outgoing edges
PathSequence of vertices connected by edges
CyclePath that starts and ends at same vertex
Connected GraphEvery vertex is reachable
Disconnected GraphSome vertices are not connected
Self LoopEdge from a vertex to itself
Parallel EdgesMultiple edges between same vertices

Data Structure for Graph Representation

Adjacency Matrix

An Adjacency Matrix is a 2D array of size V × V, where:

  • 1 indicates edge exists
  • 0 indicates no edge

Example

Graph with vertices A, B, C

ABC
A011
B100
C100

Characteristics

  • Matrix is symmetric for undirected graphs
  • Diagonal elements represent self loops

Time & Space Complexity

  • Space: O(V²)
  • Edge check: O(1)

Advantages

  • Simple representation
  • Fast edge lookup

Disadvantages

  • Wastes memory for sparse graphs

Adjacency List

An Adjacency List stores a list of adjacent vertices for each vertex.

Example

AB → C BA C → A

Time & Space Complexity

  • Space: O(V + E)
  • Edge check: O(deg(V))

Advantages

  • Memory efficient
  • Best for sparse graphs

Disadvantages

  • Edge lookup slower than matrix

Comparison: Adjacency Matrix vs Adjacency List

FeatureAdjacency MatrixAdjacency List
MemoryO(V²)O(V+E)
Edge CheckFastSlower
Sparse GraphInefficientEfficient
Dense GraphEfficientLess Efficient

Graph Traversal

Graph traversal means visiting all vertices of a graph systematically.

Depth First Search (DFS)

DFS explores as far as possible along a branch before backtracking.

Technique Used

  • Stack
  • Recursion

Algorithm (Steps)

  1. Start from a vertex
  2. Mark it as visited
  3. Visit adjacent unvisited vertex
  4. Repeat recursively
  5. Backtrack when no adjacent vertex is left

Pseudocode

DFS(v): mark v visited for each adjacent u of v: if u not visited: DFS(u)

Time Complexity

  • O(V + E)

Applications

  • Detecting cycles
  • Topological sorting
  • Path finding

Breadth First Search (BFS)

BFS explores all neighboring vertices first, then moves to next level.

Technique Used

  • Queue

Algorithm (Steps)

  1. Start from a vertex
  2. Mark it visited and enqueue
  3. Dequeue vertex and visit neighbors
  4. Enqueue unvisited neighbors
  5. Repeat until queue is empty

Pseudocode

BFS(v): mark v visited enqueue(v) while queue not empty: x = dequeue for each adjacent u of x: if u not visited: mark visited enqueue(u)

Time Complexity

  • O(V + E)

Applications

  • Shortest path (unweighted graph)
  • Level order traversal
  • Network broadcasting

DFS vs BFS Comparison

FeatureDFSBFS
Data StructureStackQueue
ApproachDepth wiseLevel wise
MemoryLessMore
Shortest PathNoYes
ImplementationRecursiveIterative

Connected Component

A Connected Component is a subgraph where:

  • All vertices are connected
  • No vertex is connected to vertices outside the component

Example

If a graph has:

  • Component 1: A, B, C
  • Component 2: D, E → Total 2 connected components

Finding Connected Components

  • Use DFS or BFS
  • Count number of times traversal starts from unvisited node

Algorithm

  1. Initialize visited array

For each vertex:

  • If not visited, run DFS/BFS
  • Increment component count

Time Complexity

  • O(V + E)

Applications

  • Social network analysis
  • Network connectivity
  • Clustering problems

Important Exam Points (MCA)

  • Graph is non-linear
  • BFS uses Queue
  • DFS uses Stack/Recursion
  • Adjacency list is best for sparse graphs
  • Connected components show independent subgraphs

Conclusion

Graphs are widely used in:

  • Computer networks
  • Maps & GPS
  • Social media
  • Operating systems

Understanding representation + traversal is very important for MCA exams and interviews.