Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
2. Content
● What is Merge Sort?
● How it happens?
● Algorithm
● Implementation
● Complexity
● Comparison with other sort algorithms
● Advantages and Disadvantages
3. What is Merge Sort?
3
What is merging?
● Mixing two array elements in ascending order to
produce a third array in ascending.
Ex:
12
15
45
10
22
30
10
12
15
22
30
45
Array A Array B
Array C
4. » Merge sort is a sorting technique based on divide and
conquer technique with worst-case time complexity
being O(n log n)
» In computer science, merge sort is an efficient, most
respected ,general-purpose and comparison-based
sorting algorithm
» In simply, merge sort first divides the array into equal
halves and then combines them in a sorted manner
4
5. 5
» To merge two sorted arrays,firstly we index both arrays
starting at zero,where the smallest element is located.
» Comparing the elements at each index,we choose the smaller
element,put it into the array that we are merging into.
» Increment the index of the smaller
element.
» By this method,we continually select
the next smallest element from the
two arrays and merge them into
sorted array.
6. How it happens?
Divide
Problem is decomposed into one or more sub problems
Conquer
Every sub problem is solved recursively
Combine
Merge the solutions of subproblems to
create the solution of the original problem
6
7. How it happens cont;
Ex:
Array Name : A
Starting index : p
Ending index : r
Array : A[p..r]
7
8. How it happens cont;
Divide
Mid point of p and r : q
the subarrays : A[p..q] and A[q+1, r]
Conquer
Sorting subarrays A[p..q] and A[q+1, r] till the base case is
reached
Combine
Combining two sorted subarrays A[p..q] and A[q+1, r]
8
11. public class MergeSortEasy {
private static void mergesort(int low, int high){
if(low < high){
int middle = (high + low) / 2;
mergesort(low, middle);
mergesort(middle + 1, high);
merge(low, middle, high);
}
}
11
Implementation
12. 12
private static void merge(int low, int middle, int high){
for(int i = low; i <= high; i++){
helper[i] = numbers[i];
}
int i = low;
int j = middle + 1;
int k = low;
16. ·Best, worst, and average time complexity of algorithm express what the
resource usage is at least, at most and on average, respectively.
·In merge Sort algorithm it can be expressed as,
T(n)=T(n/2)+T(n/2)+ ɵ(n)
T(n) = 2T(n/2) + ɵ(n)
T(n) ={2T(n/2) + cn} (if n>1)
f(n)=cn
T(n)= ɵ(nlgn)
·In all 3 cases (worst, average and best) time complexity of Merge Sort
is ɵ(nLogn).
Time Complexity
16
17. Space Complexity
● Space complexity of merge sort is O(n).
● In every recursive call that we create an array for
merging and they take no more than O(n)space.
● When the merging is done ,these arrays are
deleted and some new ones will be created in
some other recursive call.
17
18. Comparison with other sort algorithms
•Merge sort is a stable sort and is more efficient at handling slow to
access sequential media
•Merge sort is often best choice for sorting a linked list
•Heapsort has same time bounds as merge sort (heapsort requires
only O(1) and merge sort requires O(n) )
•Relatively easy to implement merge sort in such a way it requires
only O(1) extra space
•Slow random access performance of a linked list make some other
algorithms perform poor (quick sort) and some others completely
impossible (heapsort)
18