SlideShare uma empresa Scribd logo
1 de 18
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

INTERNAL
MERGE SORT

SIMPLE
MERGE SORT

TWO -WAY
MERGE SORT

EXTERNAL
MERGE SORT

BALANCED
TWO-WAY
MERGE 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.
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
EX :I/P ->
I/P ->

6

2

4

5

1

3

Merge Sort

6

2

Divide

4

5

1

3

Merge Sort

Merge Sort

Divide

6

Divide

2

4

Merge Sort
Divide

6

5

6

1

5

Merge

1

Merge

2

4

5

6

3

1
Merge

1
Merge

O/P ->

Merge Sort

Merge Sort

2
2

3

Divide

4

Merge Sort

1

Merge Sort

Merge Sort

2

6

5

1

2

3

4

5

6

3

5
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
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
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
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

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
11

44

66

88

T2 10

20

50

90

T3 15

35

55

75

T4 22

60

65

99

T1

T

10

11

Output Tape

(c) After step 2

.... . . . . . . . . . . . . . . . . . . . . .
4 input runs on tape
11

44

66

88

T2 10

20

50

90

T3 15

35

55

75

T4 22

60

65

99

T1

T 10

11

15

20

22

35

44 50

55 60 65 66 75 88 90 99

Output Tape

4 input runs on tape
(b) After step
16
Fig :- illustration of the multi-way merge
THANK
YOU

Mais conteúdo relacionado

Mais procurados

Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sortKrish_ver2
 
Selection sort
Selection sortSelection sort
Selection sortamna izzat
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using CAshish Gaurkhede
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sortingKrish_ver2
 

Mais procurados (20)

Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Heap sort
Heap sortHeap sort
Heap sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using C
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 

Destaque

Selection sort
Selection sortSelection sort
Selection sortJay Patel
 
Leaky bucket algorithm
Leaky bucket algorithmLeaky bucket algorithm
Leaky bucket algorithmUmesh Gupta
 
Leaky Bucket & Tocken Bucket - Traffic shaping
Leaky Bucket & Tocken Bucket - Traffic shapingLeaky Bucket & Tocken Bucket - Traffic shaping
Leaky Bucket & Tocken Bucket - Traffic shapingVimal Dewangan
 

Destaque (6)

Shell sort
Shell sortShell sort
Shell sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Leaky bucket algorithm
Leaky bucket algorithmLeaky bucket algorithm
Leaky bucket algorithm
 
Mastalgia
MastalgiaMastalgia
Mastalgia
 
Leaky Bucket & Tocken Bucket - Traffic shaping
Leaky Bucket & Tocken Bucket - Traffic shapingLeaky Bucket & Tocken Bucket - Traffic shaping
Leaky Bucket & Tocken Bucket - Traffic shaping
 

Semelhante a Marge Sort

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleVivek487417
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
Quick Sort in data structure.pptx
Quick Sort in data structure.pptxQuick Sort in data structure.pptx
Quick Sort in data structure.pptxujjwalmatoliya
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 

Semelhante a Marge Sort (20)

Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting
SortingSorting
Sorting
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Quick Sort in data structure.pptx
Quick Sort in data structure.pptxQuick Sort in data structure.pptx
Quick Sort in data structure.pptx
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting
SortingSorting
Sorting
 

Último

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Último (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Marge Sort

  • 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.
  • 3. MERGE SORT INTERNAL MERGE SORT SIMPLE MERGE SORT TWO -WAY MERGE SORT EXTERNAL MERGE SORT BALANCED TWO-WAY MERGE SORT MULTI -WAY MERGE SORT
  • 4.
  • 5. 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.
  • 6. 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. EX :I/P -> I/P -> 6 2 4 5 1 3 Merge Sort 6 2 Divide 4 5 1 3 Merge Sort Merge Sort Divide 6 Divide 2 4 Merge Sort Divide 6 5 6 1 5 Merge 1 Merge 2 4 5 6 3 1 Merge 1 Merge O/P -> Merge Sort Merge Sort 2 2 3 Divide 4 Merge Sort 1 Merge Sort Merge Sort 2 6 5 1 2 3 4 5 6 3 5
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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.
  • 12.
  • 13. Example :- Run :- A sorted segment of a file, which is termed as an ascending run or simply run.
  • 14.
  • 15. 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.
  • 16. 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
  • 17. 11 44 66 88 T2 10 20 50 90 T3 15 35 55 75 T4 22 60 65 99 T1 T 10 11 Output Tape (c) After step 2 .... . . . . . . . . . . . . . . . . . . . . . 4 input runs on tape 11 44 66 88 T2 10 20 50 90 T3 15 35 55 75 T4 22 60 65 99 T1 T 10 11 15 20 22 35 44 50 55 60 65 66 75 88 90 99 Output Tape 4 input runs on tape (b) After step 16 Fig :- illustration of the multi-way merge