L11_MergeSort_QuickSort

L11_MergeSort_QuickSort - Merging Two Sorted Arrays We...

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

Merging Two Sorted Arrays We first consider the merger of two sorted arrays into one sorted array. (sorted) 9 8 5 4 (naturally sorted!) 9 8 8 6 5 4 2 1 (sorted) 8 6 2 1 Page 1 of 40

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

View Full Document
Page 2 of 40 (naturally sorted!) 5 4 2 1 4 2 1 2 1 1 1 2 6 8 4 5 8 9 1 2 6 8 4 5 8 9 1 2 6 8 4 5 8 9 1 2 8 6 9 8 5 4
* void merge(int[] result, int[] a1, int[] a2) { /* result.length = a1.length + a2. length */ int p=0, p1=0, p2=0; while (p1 < a1.length && p2 < a2.length) { if (a1[p1] < a2[p2]) result[p++] = a1[p1++]; e l s e result[p++] = a2[p2++]; } while (p1 < a1.length) result[p++] = a1[p1++];

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

View Full Document
Note: int p=0, p1=0, p2=0; a1 9 8 5 4 p1 result p a2 8 6 2 1 p2 Page 4 of 40
while (p1 < a1.length && p2 < a2.length) { if (a1[p1] < a2[p2]) result[p++] = a1[p1++]; e l se result[p++] = a2[p2++]; } a1 9 8 5 4 p1 1 result of 40 1 2 8 6 a2 p

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

View Full Document
while (p1 < a1.length && p2 < a2.length) { if (a1[p1] < a2[p2]) result[p++] = a1[p1++]; e l se result[p++] = a2[p2++]; } a1 9 8 5 4 p1 1 2 result 1 8 6 2 a2 p
while (p1 < a1.length && p2 < a2.length) { if (a1[p1] < a2[p2]) result[p++] = a1[p1++]; e l se result[p++] = a2[p2++]; } 4 5 8 9 1 2 8 6 1 2 result 4 a1 a2 p p1

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

View Full Document
while (p1 < a1.length && p2 < a2.length) { if (a1[p1] < a2[p2]) result[p++] = a1[p1++]; e l se result[p++] = a2[p2++]; } 4 9 8 5 1 2 result a1 5 4 p1 p a2 8 6 2 1
while (p1 < a1.length && p2 < a2.length) { if (a1[p1] < a2[p2]) result[p++] = a1[p1++]; e l se result[p++] = a2[p2++]; } Page 9 of 40 4 5 8 9 1 2 6 8 6 1 2 result a2 p1 p a1 p2

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

View Full Document
while (p1< a1.length) result[p++] = a1[p1++]; while (p2< a2.length) result[p++] = a2[p2++]; 9 8 5 4 1 2 6 8 1 2 result 5 4 8 a2 p p2 6 p1 Page 10 of 40
The Merge Sort Algorithm In general, if a1 and a2 are already sorted , then the merged result will be sorted naturally. (sorted) 9 8 5 4 (naturally sorted!) 9 8 8 6 5 4 2 1 (sorted) 8 6 2 1 Page 11 of 40

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

View Full Document
This is the basic idea of the merge sort algorithm: First, if the array is empty, then it is already sorted. Otherwise, if the array has only 1 element, then it is
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 40

L11_MergeSort_QuickSort - Merging Two Sorted Arrays We...

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

View Full Document
Ask a homework question - tutors are online