Merge_Sorting
Merge_Sorting
Presented by
Sehar Amjad Iqra Sarooj
M Zohaib Arshad Talha Kursheed
Topics to be covered
1. Introduction
2. How does it work
3. Steps Involved
4. Manual Run Through (Dry Run)
5. Merge Sort Implementation (Program)
6. Why Merge sort
7. Advantages and Disadvantages
8. Summary
Introduction
• The Merge Sort algorithm is a DIVIDE- AND - CONQUER
algorithm.
• Sorts an array by first breaking it down into smaller arrays, and
then building the array back together correctly so that it is sorted.
Key Features:
1. Stable sort
2. Time complexity O(n log n)
3. Works by recursively dividing and merging
Steps Involved
Divide: The algorithm starts with breaking up the array into
smaller and smaller pieces until one such sub-array only consists of
one element.
Conquer: The algorithm merges the small pieces of the array back
together by putting the lowest values first, resulting in a sorted
array.
Combine: Combine (merge) the sorted sub-arrays into a single
sorted array.
How does it work
• Divide the unsorted array into two sub-arrays, half the size of the
original.
• Continue to divide the sub-arrays as long as the current piece of
the array has more than one element.
• Merge two sub-arrays together by always putting the lowest value
first.
• Keep merging until there are no sub-arrays left.
Manual Run Through (Dry Run)
MergeSortImplementation
Why Merge sort
• Compared to insertion sort merge sort is faster.
• On small inputs, insertion sort may be faster. But for large enough
inputs, merge sort will always be faster, because its running time
grows more slowly than insertion sorts.
Advantages and Disadvantages
ADVANTAGES DISADVANTAGES
Stable: Keeps the order of equal • Space complexity: Merge sort requires
elements. additional memory to store the merged
Reliable: Always sorts in O(N log sub-arrays during the sorting process.
N) time. • Not in-place: Merge sort is not an in-
Works for Large Datasets: place sorting algorithm, which means it
Consistent performance even with requires additional memory to store the
big inputs. sorted data. This can be a disadvantage
in applications where memory usage is a
concern.
• Slower than QuickSort in general.
QuickSort is more cache friendly
because it works in-place.
Summary
•Merge Sort splits, sorts, and merges.
•Guarantees O(n log n) performance.
•Best for when stability and predictable performance are important.