marge sort
Post on 22-Nov-2014
510 Views
Preview:
DESCRIPTION
TRANSCRIPT
MERGE SORT
ANKIT S. CHITNAVIS
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.
MERGE SORT
INTERNALMERGE SORT
EXTERNALMERGE SORT
SIMPLE MERGE SORT
TWO -WAY MERGE
SORT
BALANCED TWO-WAYMERGE
SORT
MULTI -WAY MERGE
SORT
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.
Div
ide
Con
qure
Com
bine
..........................................................................
...................................
...................................
...................................
...................................
Sorted List
l r
[(l+r)/2]
Sort this left-part by merge sortSort this right-part by merge sort
Merge the two list
Fig. Divide-and-conqure strategy in the merge sort
Merge
Merge
Merge
Merge
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge SortDivide
DivideDivide
Divide
Divide
6 2 4 5 1 3
6 2 4 5 1 3
6 2 4
6 2
6 2
2 6
2 4 6
5 1 3
5 1
5 1
1 5
1 3 5
1 2 3 4 5 6
4 3
Merge Sort
Merge Sort
Merge Sort
EX :-
I/P ->I/P ->
O/P ->
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) then2. Return3. else
4. Mid=[(l+r)/2]5. MergeSort(A[l…mid])6. MergeSort(A[mid+1…r])7. Merge(A,L,mid,r)8. Endif9. Stop
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.
i j k l
qP
Scan is doneScan is done
Ascending run at right
Ascending run at left
Stored merge sequneces
Stored merge sequneces
(a) Source list
(b) Destination list
EX :-
44 99
57 63 77 55 88 22 96 33 1
1 66A
A
B
B
Input List->
Auxiliary List ->
<- output List
(a) After Pass 1
(b) After Pass 2
(c) After Pass 3
11 22
33 44 55 57 63 66 77 88 9
6 99
44 66
99 22 55 88 96 77 63 57 3
3 11
A
B 11 33
44 57 63 66 77 96 99 88 5
5 22
44 66
99 22 55 88 96 77 63 57 3
3 11
11 33
44 57 63 66 77 96 99 88 5
5 22
3
1 1
11 1
11 1
1
2 2
22
2
3 3
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.
Example :-
Run :- A sorted segment of a file, which is termed as an ascending run or simply run.
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.
Example :-
11 44 66 88
10 20 50 90
15 35 55 75
22 60 65 99
11 44 66 88
10 20 50 90
15 35 55 75
22 60 65 99
10
T1
T2
T3
T4
T1
T2
T3
T4
4 input runs on tape
4 input runs on tape
Output Tape
Output Tape
T
T
(a) Initial Status
(b) After step 1
11 44 66 88
10 20 50 90
15 35 55 75
22 60 65 99
10
11
11 44 66 88
10 20 50 90
15 35 55 75
22 60 65 99
10
11
15
20
22
35
44
50
55
60
65
66
75
88
90
99
T1
T2
T3
T4T1
T2
T3
T4
. . . . . . . . . . . . . . . . . . . . . . . . .
4 input runs on tape
4 input runs on tape
Output Tape
Output Tape
T
T
(c) After step 2
(b) After step 16
Fig :- illustration of the multi-way merge sort.
THANK
YOU
top related