Post on 02-Jun-2015
TOPIC :-
MERGE SORT
B A S I C C O N C E P T O F M E R G E S O R T
I. Uses divide and conquer technique.
II. array is divided into sub-array.
III. Sub-arrays merged to get sorted result.
DIVIDE AND CONQUER
Divide and conquer method for algorithm design:
• Divide: Large problem is divided into sub-problems
• Conquer: recursively solve the sub-problems
• Combine: • Take the solutions to the sub-problems• “merge” these solutions into final solution
3
DRY RUN OF PROGRAM
4
CONTD.
5
CONTD.
6
CONTD.
7
CONTD.
8
CONTD.
9
CONTD.
10
CONTD.
11
CONTD.
12
CONTD.
13
CONTD.
14
CONTD.
15
CONTD.
16
CONTD.
17
CONTD.
18
CONTD.
19
CONTD.
20
CONTD.
21
CONTD.
22
CONTD.
23
CONTD.
24
AT THE END SORTED
25
MERGING FUNCTION void merge ( int , int , int ) ;
void merge _ sort(int low , int high)
{
int mid;
if(low<high)
{
mid=( low + high)/2 ;
merge _ sort ( low , mid) ;
merge _ sort(mid+1,high);
merge ( low , mid , high) ;
}
}
IMPLEMENTATION
SUMMARY OF SORTING ALGORITHMS
Algorithm Time Notes
selection-sort O(n2) in-place slow (good for small inputs)
insertion-sort O(n2) in-place slow (good for small inputs)
quick-sort O(n log n) expected
in-place, randomized fastest (good for large inputs)
merge-sort O (n log n) sequential data access fast (good for huge inputs)
ANY QUESTION…