UNIVERSITY OF SOUTHERN MINDANAO
Merge Sort Algorithm
Presented by:
GROUP 5
Introduction to Merge Sort
•Merge Sort is a Divide and
Conquer algorithm.
•It divides the input array into
halves, recursively sorts them,
and then merges the sorted
halves.
•Time Complexity: O(n log n)
Introductio 2
n
•Space Complexity: O(n)
Introductio 2
n
Working of Divide and Conquer
Algorithm
The three steps: Divide, Conquer and
Merge
1. Divide: Split array into two halves.
2. Conquer: Recursively sort each half.
3. Combine: Merge sorted halves into
Steps of Merge 3
one.
Steps of Merge 3
Illustration of Merge Sort
Splitting arrays
step- by-step.
Merging back
sorted arrays.
Illustration 4
Practice Exercise
Array: [38, 27, 43, 3, 9,
82, 10]
Practice 5
Answer key:
• Step 1: Divide into halves
• Left: [38, 27, 43, 3]
• Right: [9, 82, 10]
• Step 2: Recursively divide halves
• Left → [38, 27], [43, 3]
• Right → [9, 82], [10]
• Step 3: Final Merge
• Left: [3, 27, 38, 43]
• Right: [9, 10, 82]
• Combined: [3, 9, 10, 27, 38, 43, 82]
Soluti 6
Recurrence Relation of Merge
Sort:
T(n): Total time taken to sort an array of size n.
2T(n/2): Time taken to recursively sort the two
halves of the array. Each half has n/2 elements,
so there are two recursive calls.
O(n): Time taken to merge the two sorted
Recurre 7
halves of the array.
Recurre 7
Complexity Analysis of Merge Sort
Time Complexity:
Best Case: O(n log n), array is already
sorted or nearly sorted.
Average Case: O(n log n), array is
randomly ordered.
Worst Case: O(n log n), array is sorted
in reverse order.
Comple 8
Complexity Analysis of Merge Sort
Space Complexity:
Auxiliary
Space:
Requires O(n)
auxiliary space
for temporary
arrays during
merging.
Complexity 9
Pseudocode for Merge Sort
Pseudoc 1
Pseudocode for Merge Sort
Pseudoc 1
Merge Sort for Linked Lists
Special Use Cases:
• Efficient for linked lists due to sequential
access.
• Eliminates overhead of random memory
access in arrays.
Merge for Linked 1
Real-World Applications
• Large datasets: Sorting data that doesn’t
fit into
memory.
• Linked Lists: Sorting efficiently without
random access.
• E-commerce: Sorting products or
customer records.
• Data Processing: Organizing large
Applicati 1
chunks of data for analysis.
Applicati 1
Advantages of Merge Sort
•Guaranteed Efficiency: Always O(n
log n).
•Stable Sort: Maintains order
of equal elements.
•Best for Linked Lists:
Doesn't rely on random
Advanta 1
access.
Advanta 1
Disadvantages of Merge Sort
•Memory Usage: Needs additional
space for temporary arrays.
•Not In-Place: Doesn't sort the
original array directly.
•Not Ideal for Small Lists: Slower
than Quick Sort for small inputs.
Disadvant 1
Merge Sort Vs. other Algorithms
Compari 1
Summary
• Merge Sort is robust and reliable
for large datasets.
• Important algorithm in computer science.
• A foundation for learning advanced
sorting techniques.
Summ 1
References:
• GeeksforGeeks. (n.d.). Merge Sort. Retrieved
November 28, 2024, from
https://www.geeksforgeeks.org/merge-sort/
• GeeksforGeeks. (n.d.). Merge Sort Explained with
Algorithm and Examples. Retrieved November 28,
2024,
from https://www.geeksforgeeks.org/merge-sort/
• OpenAI. (2024). ChatGPT (November 28 version)
[Large language model]. Retrieved from
https://chat.openai.com/
referen 2
• Burd, B. (2017). Java for dummies (5th ed.). For Dummies.
referen 2
QUESTIONS?
19