\
  MergeSort(A[],p,r)
    1.   If p < r
    2.     q = (p+r)/2
    3.     MergeSort(A, p, q)
    4.     MergeSort(A, q+1, r)
    5.     Merge(A, p, q, r)
    1.   n1 = q-p+1 & n2 = r-q
    2.   create L[1..n1], R[1..n2]
    3.   i =0 & j = 0
    4.   while i< n1 & j< n2
    5.   If L[i]<= R[i]
    6.   A[i] <- L[i]
    7.   i++
    8.   Else:
    9.   A[i] <- R[i]
    10.   i++
    11.   while i< n1
    12.   A[i] <- L[i]
    13.   i++
    14.   while j< n2
    15   A[i] <- R[i]
    16.   j++
    6.   End If
|
|
Space | ||
| Best Case | Average Case | Worst Case | |
What is the working mechanism of Merge Sort ?
Merge sort is a divide-and-conquer algorithm that works by dividing an array into two halves, sorting each half
separately, and then merging them back together.
Is Merge sort a stable,
in-place sorting algorithm or an out-of-place sorting algorithm?
Merge sort is an out-of-place sorting algorithm. It requires additional memory space to store the subarrays being merged during the sorting process.
It is Stable.
How does the number of elements impact the efficiency of Merge sort?
The time complexity of merge sort is O(n log n), where n is the number of elements in the array.
When number of elements is small it may not be better than other algos such as insertion sort but when no. of elements
INCREASES it becomes MORE EFFICIENT as compared to others as it's time complexity grows slower then other algorithms.
What are advantages and disadvantages of Merge Sort?
Advantages: