Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

MERGE

SORT
ALGORIT
HM
Samriddhi
Singha
AGENDA

What is Merge Sort Working of Merge Sort


Algorithm Algorithm

Pseudocode of Merge Time Complexity of


Sort Algorithm Merge Sort Algorithm

Advantages of Merge Drawbacks of Merge


Sort Sort Algorithm
What is Merge Sort Algorithm?

7 3 5 2
• Merge Sort is one of the most
efficient sorting algorithms.

• It is based on the divide and conquer 7 3 5 2


strategy.

• Merge Sort continuously cuts down a


list into numerous sub lists until each 5
7 3 2
sub list contains only one entry, then
merges those sub lists into a sorted
list.
2 3 5 7
Working of Merge Sort Algorithm

The Merge Sort Algorithm can be implemented in two ways

Top Down Approach Bottom-Up Approach

Iterative process is used in the


The top-down merge sort approach is Bottom-Up approach. It starts with a
methodology which uses recursion “single-element” array and then joins
mechanism. It starts at the top and two neighboring items while also
proceeds downwards and split the array sorting them. The combined –sorted
into two, make a recursive call and merge arrays are combined and sorted again
the results, until one gets to the bottom of until only one single unit of sorted
the array. array remains.
Working of Merge Sort Algorithm

Top Down Approach Bottom-Up Approach

7 3 5 2 7 3 5 2

3 7 2 5
7 3 5 2

3 2 5 7
7 3 5 2

2 3 5 7
2 3 5 7
Pseudocode of Merge Sort Algorithm

MERGE (A,p,q,r)
1. N1=q-p+1
MERGE_SORT (A,p,r)
2. N2=r-q
3. Let L[1..n1+1] and R[1..n2+1] be new arrays
1. If p<r 4. for i=1 to n1
5. L[i]=A[p+i-1]
6. for j=1 to n2
2. q= (p+r)/2
7. R[j]=A[q+j]
8. L[n1+1]= infinite
3. MERGE_SORT (A,p,q) 9. R[n2+1]=infinite
10. i=1
11. J=1
4. MERGE_SORT (A,q+1,r)
12. for k=p to r
13. if L[i]<=R[j]
5. MERGE_SORT (A,p,q,r) 14. A[k]=L[i]
15. i=i+1
16. else A[k]=R[j]
6. End
17. j=j+1
18. End
Complexity of Merge Sort Algorithm

Time Complexity

• Merge Sort’s time complexity is


O(n*log n) in all three situation , because it
always divides the array into two halves and
Best Case merges the two halves in linear time.
O(n*log n)
• It takes upto same amount of space as the
unsorted array.As a result, it’s not a good
Worst Case idea to use it to search big, unsorted array
O(n*log n)

Average Case
O(n*log n)

The number of elements being sorted is given by n.


Advantages of Merge Sort Algorithm

Merge Sort is a highly efficient sorting

1 3
algorithm that has proven to be extremely One of the key advantages of merge sort
powerful in listing applications. is it’s stability

Merge sort also has the advantage of

2 4
Another advantage of merge sort is it’s
stability to handle large data sets being a comparison-based sorting
efficiently algorithm

Comparing merge sort with other sorting

5 algorithms, such as quick sort, reveals it’s


superiority in many cases.
Drawbacks of Merge Sort Algorithm

For smaller assignments, merge sort algorithm is slower


than the other sorting algorithms.

For the temporary array, the merge sort technique


requires an additional memory space of O(n)

Even if the array is sorted, merge sort algorithm goes


through the entire procedure
Thank You

You might also like