L10_sort_supp

L10_sort_supp - Merge Sort COMP152 Spring 2010 D.Y. Yeung...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 152 1 Merge Sort Recursive sorting strategy. Let’s look at merge(. . ) first. COMP152 Spring 2010 D.Y. Yeung
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 152 2
Background image of page 2
COMP 152 3 9 10 15 16 5 7 8 11 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) p q r l Temporary array T Because of the recursive nature of the mergesort, we know that the end of the first sub-array is just before the beginning of the second array, so . . l = q -1 .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 152 4
Background image of page 4
COMP 152 5 9 10 15 16 5 7 8 11 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) p q r l Temporary array T k=p i=0 Just compare elements at p and q and copies the smaller into position i in the temp array T. If A[p] <= A[q] p=p+1 else q=q+1. i = i + 1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 152 6 9 10 15 16 5 7 8 11 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) p q r l 5 Temporary array T k=p i=0 A[q] is smallest T[i] = A[q] i++, q++
Background image of page 6
COMP 152 7 9 10 15 16 5 7 8 11 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) p r l 5 7 Temporary array T k=p q i=1 A[q] is smallest T[i] = A[q] i++, q++
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 152 8 9 10 15 16 5 7 8 11 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) p r l 5 7 8 Temporary array T k=p i=2 A[q] is smallest T[i] = A[q] i++, q++ q
Background image of page 8
COMP 152 9 9 10 15 16 5 7 8 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) p l 5 7 8 9 Temporary array T k=p i=3 11 r q A[p] is smallest T[i] = A[p] i++, p++
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 152 10 9 10 15 16 5 7 8 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) l 5 7 8 9 10 Temporary array T k=p 11 r q p i=4 A[p] is smallest T[i] = A[p] i++, p++
Background image of page 10
COMP 152 11 9 10 15 16 5 7 8 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) l 5 7 8 9 10 Temporary array T k=p r q p i=5 A[q] is smallest T[i] = A[q] i++, q++ (STOP q<= r is false!) q Stop i=6
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 152 12 COPY Remainder of arrays (if anything is there)
Background image of page 12
COMP 152 13 9 10 15 16 5 7 8 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) l 5 7 8 9 10 11 15 Temporary array T k=p r p i=6 q
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 152 14 9 10 15 16 5 7 8 Array A MERGING TWO SORTED Sub-Arrays . . . Parameters passed Merge(A, p, q, r ) l 5 7 8 9 10 11 15 16 Temporary array T k=p r i=7 q p p Stop No iterations
Background image of page 14
COMP 152 15
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 40

L10_sort_supp - Merge Sort COMP152 Spring 2010 D.Y. Yeung...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online