SlideShare a Scribd company logo
G R Lockwood, 2006 1
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Sort the sequence 6, 5, 8, 3, 2, 7, 1Sort the sequence 6, 5, 8, 3, 2, 7, 1
using merge sortusing merge sort
G R Lockwood, 2006 2
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Split the sequenceSplit the sequence
atat mm==((ii++jj)/2)/2
 ii=1,=1, jj=7,=7, mm=4=4
66
55
88
33
22
77
11
G R Lockwood, 2006 3
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Make a recursiveMake a recursive
call to merge sortcall to merge sort
 mm :=:= ((ii++jj)/2)/2==
2.52.5=2=2
66
55
88
33
22
77
11
G R Lockwood, 2006 4
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Now sorting thisNow sorting this
partpart
 ii=1,=1, jj=2,=2, mm=1 and=1 and
make a recursivemake a recursive
callcall
66
55
88
33
22
77
11
G R Lockwood, 2006 5
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Sequence of lengthSequence of length
1 is already sorted,1 is already sorted,
so returnso return
66
55
88
33
22
77
11
G R Lockwood, 2006 6
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Sequence of lengthSequence of length
1 is sorted1 is sorted
66
55
88
33
22
77
11
G R Lockwood, 2006 7
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Return control andReturn control and
merge these 2 sub-merge these 2 sub-
sequences intosequences into
correct ordercorrect order
66
55
88
33
22
77
11
G R Lockwood, 2006 8
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Now sort theNow sort the
second part of thissecond part of this
subsequencesubsequence
55
66
88
33
22
77
11
G R Lockwood, 2006 9
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Sequence of lengthSequence of length
1 is sorted1 is sorted
55
66
88
33
22
77
11
G R Lockwood, 2006 10
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Sequence of lengthSequence of length
1 is sorted1 is sorted
55
66
88
33
22
77
11
G R Lockwood, 2006 11
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Merge these twoMerge these two
into correct orderinto correct order
55
66
88
33
22
77
11
G R Lockwood, 2006 12
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Return control toReturn control to
previous call.previous call.
 Now merge theseNow merge these
two sub-sequencestwo sub-sequences
into correct orderinto correct order
55
66
33
88
22
77
11
G R Lockwood, 2006 13
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 The recursive callThe recursive call
at line 7 is nowat line 7 is now
completedcompleted
 First part ofFirst part of
sequence is nowsequence is now
fully sortedfully sorted
33
55
66
88
22
77
11
G R Lockwood, 2006 14
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Control nowControl now
returns to the veryreturns to the very
first call of mergefirst call of merge
sortsort
 A recursive call isA recursive call is
now made on thenow made on the
second part of thesecond part of the
sequencesequence
33
55
66
88
22
77
11
G R Lockwood, 2006 15
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 ii=5,=5, jj=7,=7, mm=6=6
 Split sequence atSplit sequence at
item 6item 6
33
55
66
88
22
77
11
G R Lockwood, 2006 16
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Make a recursiveMake a recursive
call to sort thesecall to sort these
two itemstwo items
 Split sequence atSplit sequence at
item 5item 5
33
55
66
88
22
77
11
G R Lockwood, 2006 17
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Make anotherMake another
recursive call onrecursive call on
the first part of thisthe first part of this
subsequencesubsequence
33
55
66
88
22
77
11
G R Lockwood, 2006 18
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Sequence of lengthSequence of length
1 is sorted1 is sorted
33
55
66
88
22
77
11
G R Lockwood, 2006 19
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Make a recursiveMake a recursive
call on second partcall on second part
of sequenceof sequence
 Sequence of lengthSequence of length
1 is sorted1 is sorted
33
55
66
88
22
77
11
G R Lockwood, 2006 20
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Now merge these 2Now merge these 2
sub-sequencessub-sequences
into correct orderinto correct order
33
55
66
88
22
77
11
G R Lockwood, 2006 21
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Make a recursiveMake a recursive
call with thecall with the
remaining elementremaining element
33
55
66
88
22
77
11
G R Lockwood, 2006 22
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Sequence of lengthSequence of length
1 is sorted1 is sorted
33
55
66
88
22
77
11
G R Lockwood, 2006 23
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Now merge these 2Now merge these 2
sub-sequences intosub-sequences into
correct ordercorrect order
33
55
66
88
22
77
11
G R Lockwood, 2006 24
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Return to the firstReturn to the first
callcall
 Now merge these 2Now merge these 2
sub-sequences intosub-sequences into
correct ordercorrect order
33
55
66
88
11
22
77
G R Lockwood, 2006 25
Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort
 Merge sort isMerge sort is
completedcompleted
11
22
33
55
66
77
88

More Related Content

PPTX
Merge Sort
PPT
simple-sorting algorithms
PPTX
Merge sorting v2
PPT
PPTX
Sorting
PDF
Bin Sorting And Bubble Sort By Luisito G. Trinidad
PPTX
Merge sort algorithm
PDF
Sorting bubble-sort anim
Merge Sort
simple-sorting algorithms
Merge sorting v2
Sorting
Bin Sorting And Bubble Sort By Luisito G. Trinidad
Merge sort algorithm
Sorting bubble-sort anim

Viewers also liked (16)

PPTX
Marge Sort
ODP
Sorting Algorithm
PPT
Bubble sort
PPTX
Sorting algorithms
PPTX
Sorting techniques Anil Dutt
PPTX
Sorting (Bubble,Merge,Selection sort)
PPT
Bubble sort
PPT
3.1 bubble sort
PDF
Sorting
PPTX
bubble sorting of an array in 8086 assembly language
PPT
Bubble sort
PPTX
Advanced Sorting Algorithms
PPTX
Bubble Sort
PPT
Merge sort
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPT
Sorting
Marge Sort
Sorting Algorithm
Bubble sort
Sorting algorithms
Sorting techniques Anil Dutt
Sorting (Bubble,Merge,Selection sort)
Bubble sort
3.1 bubble sort
Sorting
bubble sorting of an array in 8086 assembly language
Bubble sort
Advanced Sorting Algorithms
Bubble Sort
Merge sort
Data Structures - Lecture 8 [Sorting Algorithms]
Sorting
Ad

More from Sumedha (20)

PPTX
Deutsch famous facts
PPTX
German music
PDF
Ada scan
PDF
Scan2
PDF
Scan1
PPT
998e6 des
PPT
Selab1 slides
DOCX
Reading skills
DOC
Negotiations
DOC
Rapport
DOC
Meeetings
DOCX
Group discussions
DOC
Small talk
PPT
Dc8c4f010f40.hanoi.towers
PPT
Is sort andy-le
DOC
An example of bubble sort written in c
PPT
Group discussion
DOCX
Group discussions eng project
PDF
Eng presentation
PDF
Computer architecture kai hwang
Deutsch famous facts
German music
Ada scan
Scan2
Scan1
998e6 des
Selab1 slides
Reading skills
Negotiations
Rapport
Meeetings
Group discussions
Small talk
Dc8c4f010f40.hanoi.towers
Is sort andy-le
An example of bubble sort written in c
Group discussion
Group discussions eng project
Eng presentation
Computer architecture kai hwang
Ad

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation theory and applications.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Machine Learning_overview_presentation.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
cuic standard and advanced reporting.pdf
Unlocking AI with Model Context Protocol (MCP)
Machine Learning_overview_presentation.pptx
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The AUB Centre for AI in Media Proposal.docx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Merge sort

  • 1. G R Lockwood, 2006 1 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sort the sequence 6, 5, 8, 3, 2, 7, 1Sort the sequence 6, 5, 8, 3, 2, 7, 1 using merge sortusing merge sort
  • 2. G R Lockwood, 2006 2 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Split the sequenceSplit the sequence atat mm==((ii++jj)/2)/2  ii=1,=1, jj=7,=7, mm=4=4 66 55 88 33 22 77 11
  • 3. G R Lockwood, 2006 3 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call to merge sortcall to merge sort  mm :=:= ((ii++jj)/2)/2== 2.52.5=2=2 66 55 88 33 22 77 11
  • 4. G R Lockwood, 2006 4 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now sorting thisNow sorting this partpart  ii=1,=1, jj=2,=2, mm=1 and=1 and make a recursivemake a recursive callcall 66 55 88 33 22 77 11
  • 5. G R Lockwood, 2006 5 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is already sorted,1 is already sorted, so returnso return 66 55 88 33 22 77 11
  • 6. G R Lockwood, 2006 6 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 66 55 88 33 22 77 11
  • 7. G R Lockwood, 2006 7 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Return control andReturn control and merge these 2 sub-merge these 2 sub- sequences intosequences into correct ordercorrect order 66 55 88 33 22 77 11
  • 8. G R Lockwood, 2006 8 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now sort theNow sort the second part of thissecond part of this subsequencesubsequence 55 66 88 33 22 77 11
  • 9. G R Lockwood, 2006 9 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 55 66 88 33 22 77 11
  • 10. G R Lockwood, 2006 10 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 55 66 88 33 22 77 11
  • 11. G R Lockwood, 2006 11 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Merge these twoMerge these two into correct orderinto correct order 55 66 88 33 22 77 11
  • 12. G R Lockwood, 2006 12 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Return control toReturn control to previous call.previous call.  Now merge theseNow merge these two sub-sequencestwo sub-sequences into correct orderinto correct order 55 66 33 88 22 77 11
  • 13. G R Lockwood, 2006 13 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  The recursive callThe recursive call at line 7 is nowat line 7 is now completedcompleted  First part ofFirst part of sequence is nowsequence is now fully sortedfully sorted 33 55 66 88 22 77 11
  • 14. G R Lockwood, 2006 14 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Control nowControl now returns to the veryreturns to the very first call of mergefirst call of merge sortsort  A recursive call isA recursive call is now made on thenow made on the second part of thesecond part of the sequencesequence 33 55 66 88 22 77 11
  • 15. G R Lockwood, 2006 15 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  ii=5,=5, jj=7,=7, mm=6=6  Split sequence atSplit sequence at item 6item 6 33 55 66 88 22 77 11
  • 16. G R Lockwood, 2006 16 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call to sort thesecall to sort these two itemstwo items  Split sequence atSplit sequence at item 5item 5 33 55 66 88 22 77 11
  • 17. G R Lockwood, 2006 17 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make anotherMake another recursive call onrecursive call on the first part of thisthe first part of this subsequencesubsequence 33 55 66 88 22 77 11
  • 18. G R Lockwood, 2006 18 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 33 55 66 88 22 77 11
  • 19. G R Lockwood, 2006 19 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call on second partcall on second part of sequenceof sequence  Sequence of lengthSequence of length 1 is sorted1 is sorted 33 55 66 88 22 77 11
  • 20. G R Lockwood, 2006 20 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now merge these 2Now merge these 2 sub-sequencessub-sequences into correct orderinto correct order 33 55 66 88 22 77 11
  • 21. G R Lockwood, 2006 21 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call with thecall with the remaining elementremaining element 33 55 66 88 22 77 11
  • 22. G R Lockwood, 2006 22 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 33 55 66 88 22 77 11
  • 23. G R Lockwood, 2006 23 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now merge these 2Now merge these 2 sub-sequences intosub-sequences into correct ordercorrect order 33 55 66 88 22 77 11
  • 24. G R Lockwood, 2006 24 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Return to the firstReturn to the first callcall  Now merge these 2Now merge these 2 sub-sequences intosub-sequences into correct ordercorrect order 33 55 66 88 11 22 77
  • 25. G R Lockwood, 2006 25 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Merge sort isMerge sort is completedcompleted 11 22 33 55 66 77 88