KEMBAR78
Merge Sort (w/ principle, algorithm, code, visualizations) | PPTX
Merge Sort
The basic idea behind merge sort is this: it tends to be a lot easier to
sort two smaller, sorted lists rather than sorting a single large, unsorted
one.
Flowchart:
1.
2.
Concept of Divide and Conquer Algorithm:
โ€ข Divide : Break the given problem into subproblems of same
type.
โ€ข Conquer : Recursively solve these subproblems.
โ€ข Combine : Appropriately combine the answers.
Code:
โ€ข https://replit.com/@JatinBhat/Merge-Sort#main.c
Characteristics of merge sort:
1) Merge sort is an External sorting algorithm (just like quick sort).
External sorting is a term for a class of sorting algorithms that can
handle massive amounts of data.
External sorting is required when the data being sorted does not fit into
the main memory of a computing device (usually RAM).
In the sorting phase, chunks of data small enough to fit in the main
memory are read, sorted, and written out to a temporary file. In the
merge phase, the sorted sub-files are combined into a single larger file.
2) Merge sort is stable.
Time complexity:
Space complexity:
Visualizers:
1. https://youtu.be/ZRPoEKHXTJg?si=ivrPbxAH6GBRyX2s
2. https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.ht
ml
3. https://www.hackerearth.com/practice/algorithms/sorting/merge-
sort/visualize/
Advantages:
1. Faster runtime: Because merge sort is generally implemented recursively that
makes it faster than the other algorithms weโ€™ve looked at thus far.
Note: Slower Iterative merge sort code:
https://www.techiedelight.com/iterative-merge-sort-algorithm-bottom-up/
2) Consistent Performance: It guarantees O(n log n) time complexity
for worst, average, and best-case scenarios. This makes it a reliable
choice for sorting large datasets.
3) Efficient for Linked Lists: Merge sort does not depend on
indices.
4) External Sorting: Merge sort is well-suited for external
sorting, where data is too large to fit into the computer's main
memory. It can efficiently merge sorted chunks of data from
external storage, minimizing the need for extensive random
access.
Disadvantage:
1) Space Complexity: Merge sort requires additional space
proportional to the size of the input array.
1) Not In-Place: In an in-place algorithm, the sorting is performed using
a constant amount of extra memory.
2) Slower on Small Datasets: less efficient compared to simpler
algorithms like insertion sort or bubble sort.
3) Complexity of Implementation: While the basic idea of merge sort is
simple, the actual implementation can be more complex than some other
sorting algorithms.
4) Not Adaptive: Merge sort does not take advantage of existing order in
the dataset. Even if the input is partially sorted, merge sort still divides
and merges the data in the same way, resulting in the same time
complexity.
5) Cache Inefficiency: The sequential access pattern of merge sort might
result in poor cache locality, especially on modern computer
architectures where cache efficiency is crucial for performance. This can
Criteria Merge Sort Selection Sort Bubble Sort
Time Complexity O(n log n) O(n^2) O(n^2)
Space Complexity O(n) (additional space) O(1) O(1)
In-Place Sorting
No (requires additional
space)
Yes Yes
Stable Sorting Yes No Yes
Adaptive No No
Yes (with optimized
version)
Suitable for Small Arrays No Yes (for small datasets) Yes (for small datasets)
Recursive Implementation Yes No No
Iterative Implementation Yes Yes Yes
Practical Performance Efficient for large datasets
Inefficient for large
datasets
Inefficient for large
datasets
Use Cases
General-purpose sorting
algorithm
Small datasets or partially
sorted data
Educational purposes,
simple sorting tasks

Merge Sort (w/ principle, algorithm, code, visualizations)

  • 1.
  • 2.
    The basic ideabehind merge sort is this: it tends to be a lot easier to sort two smaller, sorted lists rather than sorting a single large, unsorted one.
  • 3.
  • 4.
  • 5.
    Concept of Divideand Conquer Algorithm: โ€ข Divide : Break the given problem into subproblems of same type. โ€ข Conquer : Recursively solve these subproblems. โ€ข Combine : Appropriately combine the answers.
  • 6.
  • 7.
    Characteristics of mergesort: 1) Merge sort is an External sorting algorithm (just like quick sort). External sorting is a term for a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted does not fit into the main memory of a computing device (usually RAM). In the sorting phase, chunks of data small enough to fit in the main memory are read, sorted, and written out to a temporary file. In the merge phase, the sorted sub-files are combined into a single larger file.
  • 8.
    2) Merge sortis stable.
  • 9.
  • 11.
  • 12.
  • 13.
    Advantages: 1. Faster runtime:Because merge sort is generally implemented recursively that makes it faster than the other algorithms weโ€™ve looked at thus far. Note: Slower Iterative merge sort code: https://www.techiedelight.com/iterative-merge-sort-algorithm-bottom-up/
  • 14.
    2) Consistent Performance:It guarantees O(n log n) time complexity for worst, average, and best-case scenarios. This makes it a reliable choice for sorting large datasets. 3) Efficient for Linked Lists: Merge sort does not depend on indices. 4) External Sorting: Merge sort is well-suited for external sorting, where data is too large to fit into the computer's main memory. It can efficiently merge sorted chunks of data from external storage, minimizing the need for extensive random access.
  • 15.
    Disadvantage: 1) Space Complexity:Merge sort requires additional space proportional to the size of the input array. 1) Not In-Place: In an in-place algorithm, the sorting is performed using a constant amount of extra memory. 2) Slower on Small Datasets: less efficient compared to simpler algorithms like insertion sort or bubble sort. 3) Complexity of Implementation: While the basic idea of merge sort is simple, the actual implementation can be more complex than some other sorting algorithms. 4) Not Adaptive: Merge sort does not take advantage of existing order in the dataset. Even if the input is partially sorted, merge sort still divides and merges the data in the same way, resulting in the same time complexity. 5) Cache Inefficiency: The sequential access pattern of merge sort might result in poor cache locality, especially on modern computer architectures where cache efficiency is crucial for performance. This can
  • 16.
    Criteria Merge SortSelection Sort Bubble Sort Time Complexity O(n log n) O(n^2) O(n^2) Space Complexity O(n) (additional space) O(1) O(1) In-Place Sorting No (requires additional space) Yes Yes Stable Sorting Yes No Yes Adaptive No No Yes (with optimized version) Suitable for Small Arrays No Yes (for small datasets) Yes (for small datasets) Recursive Implementation Yes No No Iterative Implementation Yes Yes Yes Practical Performance Efficient for large datasets Inefficient for large datasets Inefficient for large datasets Use Cases General-purpose sorting algorithm Small datasets or partially sorted data Educational purposes, simple sorting tasks