12. MergeSort_outside

Of the elements of a and b merging two sorted

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

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

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

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

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

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

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 ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern