Sorting1.5 - Merging Two Sorted Sequences (As Linked Lists)...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Merging Two Sorted Sequences (As Linked Lists) Algorithm merge(S1, S2 , S): Input: Sorted sequences S1 and S2 and an empty sequence S, implemented as linked lists Output: Sorted sequence S containing the elements from S1 and S2 while S1 and S2 do if S1.first().element() S2 .first().element() then S.addLast(S1.remove(S1.first())) i i +1 else S.addLast(S2 .remove(S2 .first())) while S1 do S.addLast(S1.remove(S1.first())) while S2 do S.addLast(S2 .remove(S2 .first())) CSE 2011 Prof. J. Elder - 26 - Last Updated: 4/1/10 11:16 AM 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 7|2 7 CSE 2011 Prof. J. Elder 7 2479 9|4 27 2 2 9 - 27 - 9 49 4 4 Last Updated: 4/1/10 11:16 AM Execution Example Partition CSE 2011 Prof. J. Elder - 28 - Last Updated: 4/1/10 11:16 AM Execution Example (cont.) Recursive call, partition CSE 2011 Prof. J. Elder - 29 - Last Updated: 4/1/10 11:16 AM Execution Example (cont.) Recursive call, partition CSE 2011 Prof. J. Elder - 30 - Last Updated: 4/1/10 11:16 AM ...
View Full Document

This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.

Ask a homework question - tutors are online