2.
MERGE SORT :ïƒ˜ All sorting method based on merging can be divided into
two broad categories :1. Internal merge sort
2. External merge sort.
ïƒ˜ In internal merge sort , the lists under sorting are small and
assumed to be stored in the high speed primary memory.
ïƒ˜ There are two type of Internal merge sort :I. Simple merge sort.
II. Two-way merge sort.
ïƒ˜
In external merge sort, deals with very large lists of elements
such that size of primary memory is not adequate to accommodate
the entire lists in it.
ïƒ˜ There are two type of External merge sort :I. Balanced two-way merge sort.
II. Multi-way merge sort.
4.
SIMPLE MERGE SORT :ïƒ˜ The simple merge sort (or merge sort) technique closely follows the
divide â€“and-conqure paradigm.
ïƒ˜ let a list of n elements to be sorted with l and r being the position
of leftmost and rightmost element in the list.
ïƒ˜ The three tasks in this divided-and-conqure technique are as
followed :-
[
]
1. Divide :- Partition the list midway ,that is ,at (l+r)/2 into two
sub lists with (n/2) elements in each, if n is even or
[n/2] and [n/2]-1 element if n is odd .
2. Conquer :- Sort the two lists recursively using the merge sort.
3. Combine :- Merge the sort sub listed to obtain the sorted output.
5.
Combine
Conqure
Divide
l
r
.........................................................................
.
[(l+r)/2]
.................................. ..................................
.
.
Sort this left-part by merge sort Sort this right-part by merge sort
...................................
...................................
Merge the two list
Sorted List
Fig. Divide-and-conqure strategy in the merge
sort
7.
Algorithm Merge sort
Input :- An array A[lâ€¦r] where l and r are the lower and upper index
of A.
Output :- Array a[lâ€¦r] with all element arranged in ascending order .
Steps:
1. if(r<=l) then
2.
Return
3. else
4.
Mid=[(l+r)/2]
5.
MergeSort(A[lâ€¦mid])
6.
MergeSort(A[mid+1â€¦r])
7.
Merge(A,L,mid,r)
8. Endif
9. Stop
8.
Two-Way Merge Sort :ïƒ˜ The two-way merge sort is based on the principle â€˜burns the candle
at both endsâ€™ in manner similar to the scanning procedure.
ïƒ˜ In the two-way merge sorting, we examine the input list from both the
ends:
Left and Right and moving toward the middle.
(a) Source list
Scan is done
Ascending run at left
i
Stored merge
sequneces
j
P
Ascending run at right
k
Scan is done
l
q
(b) Destination list
Stored merge
sequneces
9.
EX :-
1
2
3
3
2
1
Input
List->
A
44 99 57 63 77 55 88 22 96 33 11 66
Auxiliary List
->
B
44 66 99 22 55 88 96 77 63 57 33
1
2
1
A
B
(a) After Pass 1
1
2
11
3
44 66 99 22 55 88 96 77 63 57 33
11
11 33 44 57 63 66 77 96 99 88 55 22
1
2
(b) After Pass 2
1
1
A
11 33 44 57 63 66 77 96 99 88 55 22
B
11 22 33 44 55 57 63 66 77 88 96 99
(c) After Pass 3
1
<- output
List
10.
BALANCED TOW-WAY MERGE SORT :-
ïƒ˜ The balanced two-way merge is based on combining two ascending runs
into a single ascending run.
ïƒ˜The merging-procedure can be applied to more than two runs at
each time.
ïƒ˜ That why it is two way merging.
ïƒ˜ In external balanced two-way merge all intial intial runs of equal
length possibly not the last run.
ïƒ˜ This why the â€˜ balancedâ€™ tag is used in this technique.
11.
Example :-
Run :- A sorted segment of a file, which is termed as an ascending run or simply run.
12.
MULTI-WAY MEGRE SORT :ïƒ˜ In multi-way merge sort , this procedure is extended to m-way
merging , where m (m>2) runs are combined into a single run.
ïƒ˜ In the multi-way merge sort , m input runs are stored on m tapes.
ïƒ˜ Initially all element are in ascending order on all these tapes.
ïƒ˜ In multi-way merge , we merge them together , that is ,we look at the
first element of each run and select the smallest element.
ïƒ˜ This smallest element tranferred to an output tape.
ïƒ˜This process is repeated till all runs are fully examined.
13.
Example :-
11
44
66
88
T2 10
20
50
90
T3 15
35
55
75
T4 22
60
65
99
T1
4 input runs on tape
11
44
66
20
50
90
T3 15
35
55
60
65
(a) Initial
Status
75
T4 22
Output Tape
88
T2 10
T
99
T1
4 input runs on tape
T 10
Output Tape
(b) After step 1
Be the first to comment