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

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Last Updated: 4/1/10 11:16 AM CSE 2011 Prof. J. Elder - 26 - Merging Two Sorted Sequences (As Linked Lists) Algorithm merge(S 1 , S 2 , S): Input : Sorted sequences S 1 and S 2 and an empty sequence S, implemented as linked lists Output : Sorted sequence S containing the elements from S 1 and S 2 while S 1 ± ² and S 2 ± ² do if S 1 .first().element() ³ S 2 .first().element() then S .addLast( S 1 .remove( S 1 .first())) i ´ i + 1 else S .addLast( S 2 .remove( S 2 .first())) while S 1 ± ² do S .addLast( S 1 .remove( S 1 .first())) while S 2 ± ² do S .addLast( S 2 .remove( S 2 .first()))
Image of page 1

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

View Full Document Right Arrow Icon