0% found this document useful (0 votes)
58 views59 pages

Merge Sort

Merge sort is an efficient sorting algorithm that works by dividing the array into equal halves and recursively sorting the halves until they can no longer be divided into single elements. It then combines the sorted halves back into a fully sorted array. The time complexity of merge sort is O(n log n) as it divides the problem space into smaller subproblems in a treelike manner. The space complexity is O(n) to store the sorted subarrays.

Uploaded by

Prasanth Mps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views59 pages

Merge Sort

Merge sort is an efficient sorting algorithm that works by dividing the array into equal halves and recursively sorting the halves until they can no longer be divided into single elements. It then combines the sorted halves back into a fully sorted array. The time complexity of merge sort is O(n log n) as it divides the problem space into smaller subproblems in a treelike manner. The space complexity is O(n) to store the sorted subarrays.

Uploaded by

Prasanth Mps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Merge Sort

Merge Sort

Merge sort is one of the most efficient sorting algorithms. It works on the principle
of Divide and Conquer.

In divide and conquer approach, the problem in hand, is divided into smaller sub
problems and then each problem is solved independently. When we keep on
dividing the subproblems into even smaller sub-problems, we may eventually
reach a stage where no more division is possible. Those "atomic" smallest
possible sub-problem (fractions) are solved. The solution of all sub-problems is
finally merged in order to obtain the solution of an original problem.
Algorithm
Merge sort keeps on dividing the array into equal halves until
it can no more be divided. By definition, if it is only one
element in the array, it is sorted. Then, merge sort combines
the smaller sorted arrays keeping the new array sorted too.
1 9 8 23 14 5 7 3
1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3
1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23
1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3
1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9
1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23
1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23 14 5
1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3

1 9 8 23 14 5 7 3
1 9 8 23 14 5 7 3
1 9 8 23 14 5 7 3
9 8 23 14 5 7 3

1
8 23 14 5 7 3

1 9
8 23 14 5 7 3

1 9
23 14 5 7 3

1 9 8
14 5 7 3

1 9 8 23
14 5 7 3

1 9 8 23
14 7 3

1 9 8 23 5
7 3

1 9 8 23 5 14
7 3

1 9 8 23 5 14
7

1 9 8 23 5 14 3
1 9 8 23 5 14 3 7
1 9 8 23 5 14 3 7
9 8 23 5 14 3 7

1
9 23 5 14 3 7

1 8
23 5 14 3 7

1 8 9
5 14 3 7

1 8 9 23
5 14 3 7

1 8 9 23
5 14 7

1 8 9 23 3
14 7

1 8 9 23 3 5
14

1 8 9 23 3 5 7
1 8 9 23 3 5 7 14
1 8 9 23 3 5 7 14
8 9 23 3 5 7 14

1
8 9 23 5 7 14

1 3
8 9 23 7 14

1 3 5
8 9 23 14

1 3 5 7
9 23 14

1 3 5 7 8
23 14

1 3 5 7 8 9
23

1 3 5 7 8 9 14
1 3 5 7 8 9 14 23
Code for Merge Sort
The merge sort involves two functions:-

The MergeSort() function recursively calls itself to divide the


array till size becomes one.

The Merge() function is used for merging two halves.


CODE:-https://ideone.com/Qrj2k9
Complexity Analysis

T(n/2)
T(n/2)
c*n time

T(n)=c*n+2*(T(n/2))
RECURRENCE RELATION
T(n)=c*n+2*T(n/2)
RECURRENCE RELATION
T(n)=c*n+2*T(n/2)
T(n/2) = 2*T(n/4) + c*(n/2)
RECURRENCE RELATION
T(n)=c*n+2*T(n/2)
T(n/2) = 2*T(n/4) + c*(n/2)
T(n) = 2 [ 2*T(n/4) + c*(n/2) ] + c*n
T(n) = 4*T (n/4) + 2c*n
RECURRENCE RELATION
T(n)=c*n+2*T(n/2)
T(n/2) = 2*T(n/4) + c*(n/2)
T(n) = 2 [ 2*T(n/4) + c*(n/2) ] + c*n
T(n) = 4*T (n/4) + 2c*n
Similarly, T(n) = 8*T (n/8) + 3*c*n
RECURRENCE RELATION
T(n)=c*n+2*T(n/2)
T(n/2) = 2*T(n/4) + c*(n/2)
T(n) = 2 [ 2*T(n/4) + c*(n/2) ] + c*n
T(n) = 4*T (n/4) + 2c*n
Similarly, T(n) = 8*T (n/8) + 3*c*n
General T(n) = 2k * T(n/2k) + k*c*n
T(n) = 2k * T(n/2k) + k*c*n
If we consider n=2k ,Then k= log2n.
On substituting the values of n and k in the general equation
we get.

T(n) = n * T(1) + c*n*log2n.

Therefore the time complexity of Merge sort is O(n*log2n ).

Space Complexity of Merge sort is O(n)


Properties of Merge Sort
Merge Sort is a stable sort which means that the same element in an array
maintain their original positions with respect to each other.
Properties of Merge Sort
The standard merge sort on an array is not an in-place algorithm, since it requires
O(N) additional space to perform the merge.

Merge Sort is an “Non-Adaptive” Sorting algorithm,which means even if the array


is sorted , time complexity will still be O(nlogn).

You might also like