{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Of the elements of a and b merging two sorted

Info iconThis preview shows pages 4–9. Sign up to view the full content.

View Full Document Right Arrow Icon
of the elements of A and B Merging two sorted sequences, each with n / 2 elements and implemented by means of a doubly linked list, takes O ( n ) time Algorithm merge ( A, B ) Input sequences A and B with n / 2 elements each Output sorted sequence of A B S empty sequence while A.empty () B.empty () if A.front () < B.front () S.addBack ( A.front ()); A.eraseFront (); else S.addBack ( B.front ()); B.eraseFront (); while A.empty () S.addBack ( A.front ()); A.eraseFront (); while B.empty () S.addBack ( B.front ()); B.eraseFront (); return S
Background image of page 4

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

View Full Document Right Arrow Icon
© 2004 Goodrich, Tamassia Merge Sort 5 Merge-Sort Tree An execution of merge-sort is depicted by a binary tree each node represents a recursive call of merge-sort and stores unsorted sequence before the execution and its partition sorted sequence at the end of the execution the root is the initial call the leaves are calls on subsequences of size 0 or 1 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4
Background image of page 5
© 2004 Goodrich, Tamassia Merge Sort 6 Execution Example Partition 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
Background image of page 6

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

View Full Document Right Arrow Icon
© 2004 Goodrich, Tamassia Merge Sort 7 Execution Example (cont.) Recursive call, partition 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
Background image of page 7
© 2004 Goodrich, Tamassia Merge Sort 8 Execution Example (cont.) Recursive call, partition 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6
Background image of page 8

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

View Full Document Right Arrow Icon
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}