The document presents an overview of the merge sort algorithm, which is a divide and conquer sorting technique with a time complexity of O(n log n). It explains the merge sort process, the algorithm, and its efficiency in sorting, making it stable and suitable for linked lists. The document also discusses the advantages and drawbacks of merge sort compared to other sorting algorithms like heap sort.
MERGE SORT :
Mergesort was invented by John Von Neumann
(1903 - 1957)
Merge sort is divide & conquer technique of sorting
element
Merge sort is one of the most efficient sorting
algorithm
Time complexity of merge sort is O(n log n)
6.
DIVIDE & CONQUER
â˘DIVIDE : Divide the unsorted list into two sub lists of
about half the size
⢠CONQUER : Sort each of the two sub lists recursively.
If they are small enough just solve them in a straight
forward manner
⢠COMBINE : Merge the two-sorted sub lists back into
one sorted list
7.
DIVIDE & CONQUERTECHNIQUE
a problem of size n
sub problem 1 of size n/2
sub problem 2 of
size n/2
a solution to sub
problem 1
a solution to sub
problem 2
a solution to the
original problem
8.
MERGE SORT PROCESS:
⢠The list is divided into two equal (as equal as
possible) part
⢠There are different ways to divide the list into two
equal part
⢠The following algorithm divides the list until the list
has just one item which are sorted
⢠Then with recursive merge function calls these
smaller pieces merge
9.
MERGE SORT ALGORITHM:
⢠Merge(A[],p , q , r)
{ n1 = q â p + 1
n2 = r â q
Let L[1 to n1+1] and R[1 to n2+1] be new array
for(i = 1 to n1)
L[i] = A[p + i - 1]
for(j = 1 to n2)
R[j] = A[q + j]
L[n1 + 1] = infinity
R[n2 + 1] = infinity
10.
for(k = pto r)
{ if ( L[i] <= R[j])
A[k] = L[j]
i = i + 1
else
A[k] = R[j]
j = j + 1
}
11.
Merge sort (A,p, r)
{ if (p < r) // check for base case
q = [ (p + r)/2 ] // divide step
Merge sort (A, p, q) // conquer step
Merge sort (A, q+1, r) // conquer step
Merge sort (A, p, q, r) // conquer step
}
IMPLEMENTING MERGE SORT:
There are two basic ways to implement
merge sort
In place : Merge sort is done with only the
input array
Double storage : Merging is done with a
temporary array of the same size as the input
array
23.
MERGE-SORT ANALYSIS :
Time,merging
log n levels
Total time for merging : O (n log n)
Total running time : Order of n log n
Total space : Order of n
n/2 n/2
n/4 n/4 n/4 n/4
n
24.
Performance of MERGESORT:
⢠Unlike quick sort, merge sort guarantees O (n log n)in
the worst case
⢠The reason for this is that quick sort depends on the
value of the pivot whereas merge sort divides the list
based on the index
⢠Why is it O (n log n ) ?
⢠Each merge will require N comparisons
⢠Each time the list is halved
⢠So the standard divide & conquer recurrence applies
to merge sort
T(n) = 2T * n/2 + (n)
25.
FEATURES OF MERGESORT :
⢠It perform in O(n log n ) in the worst case
⢠It is stable
⢠It is quite independent of the way the initial list is organized
⢠Good for linked lists. Can be implemented in such a way that
data is accessed sequentially.
Drawbacks :
It may require an array of up to the size of the original list.
This can be avoided but the algorithm becomes significantly
more complicated making it worth while.
Instead of making it complicated we can use HEAP SORT which
is also O(n log n ). But you have to remember that HEAP SORT is not
stable in comparison to MERGE SORT