L10_sort_supp

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

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

COMP 152 1 Merge Sort Recursive sorting strategy. Let’s look at merge(. . ) first. COMP152 Spring 2010 D.Y. Yeung

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

View Full Document
COMP 152 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 .

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

View Full Document
COMP 152 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

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

View Full Document
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++
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++

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

View Full Document
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
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++

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

View Full Document
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++
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

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

View Full Document
COMP 152 12 COPY Remainder of arrays (if anything is there)
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

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

View Full Document
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
COMP 152 15

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online