You are on page 1of 14

What is Merge sort?

• Merge sort is a sorting technique


which works based on DIVIDE
AND CONQUER method
• This method uses recursion
principle
• Merge sort is the typical example
of Divide and Conquer
Divide and Conquer

This is a 3 phase technique

3. Divide step:- Divide input data S


into disjoint subsets S1 ,S2,…
4. Recur:- Solve the Subsets
S1,S2,…
5. Combine:- Combine the subset
solutions
How it is Merge sort
done?
Merge sort is done in 3 main steps:-
1. Subdividing the main problem till
the problem becomes small
enough.
2. Solving the sub problem
recursively
3. Combining the solutions (merging)
to get the complete solution.
Technical stuff
The Algorithm for Merge_sort() can
be written as:-
Merge_sort(input, start, limit)
{ if(start<limit) //if problem is large enough
{ div=(start + limit)/2;// here divided into 2
subpbms
Merge_sort(input, start, div);
Merge_sort(input, div+1,limit);
Merge(input, output, start, div, limit);
}
}
Technical stuff
Similarly Algorithm for Merge() can
be written as:-
Merge(input, output, start, div, limit)
{ j=0;
while(start<div||div<limit)
{
if (input[start] < input[div]) // smaller one first
{output[j] = input[limit]; start++;}
else {output[j] = input[div]; div++}
j++;
}}
Time Complexity

There are three phases in this sorting


techinique;
The division phase, the conquer phase and
finally combining the sub solutions.
Division an combine takes O(n) while the
Conquer step is O(log n)
That is:- T(n) = 2T (n/2) + O(n)
yielding a net time of O(nlogn)
Time Complexity
Comparison
Non Divide and Conquer
algorithms
Why this?

1.Speed
2.High efficiency
3.Better than Heap sort in
the case of large arrays
Short-comings

1.Efficiency not high for


small arrays
2.Additional memory
needed for the merge()
3.Usage of recursion means
need of dynamic memory
References

3. www.wikipedia.org
4. Introduction to algorithms
By Thomas Cormen
Charles Leiserson
Ronald Rivest
7. http://linux.wku.edu/~lamonml/algor/sort/sort.html
8. http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/m

You might also like