12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

526 Chap. 15 Lower BoundsABAorAFigure 15.5 Organizing comparisons for sorting five elements. First we ordertwo pairs of elements, <strong>and</strong> then compare the two winners <strong>to</strong> form a binomial treeof four elements. The original loser <strong>to</strong> the root is labeled A, <strong>and</strong> the remainingthree elements form a sorted chain (forming a sorted chain of four elements). Wethen insert element B in<strong>to</strong> the sorted chain. Finally, we put A in<strong>to</strong> the resultingchain <strong>to</strong> yield a final sorted list.1234Figure 15.6 Merge insert sort for ten elements. First five pairs of elements arecompared. The five winners are then sorted. This leaves the elements labeled 1-4<strong>to</strong> be sorted in<strong>to</strong> the chain made by the remaining six elements.comparisons in the worst case for lists of size 4, 5, 6, or 7. So we pick the order ofinserts <strong>to</strong> optimize the binary searches, which means picking an order that avoidsgrowing a sublist size such that it crosses the boundary on list size <strong>to</strong> require anadditional comparison. This sort is called merge insert sort, <strong>and</strong> also known asthe Ford <strong>and</strong> Johnson sort.For ten elements, given the poset shown in Figure 15.6 we fold in the lastfour elements (labeled 1 <strong>to</strong> 4) in the order Element 3, Element 4, Element 1, <strong>and</strong>finally Element 2. Element 3 will be inserted in<strong>to</strong> a list of size three, costing twocomparisons. Depending on where Element 3 then ends up in the list, Element 4will now be inserted in<strong>to</strong> a list of size 2 or 3, costing two comparisons in eithercase. Depending on where Elements 3 <strong>and</strong> 4 are in the list, Element 1 will now beinserted in<strong>to</strong> a list of size 5, 6, or 7, all of which requires three comparisons <strong>to</strong> placein sort order. Finally, Element 2 will be inserted in<strong>to</strong> a list of size 5, 6, or 7.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!