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.

270 Chap. 7 Internal Sortingcompared with X). Again, there are two possibilities. If Z is less than X, then theseitems should be swapped (the left branch). If Z is not less than X, then InsertionSort is complete (the right branch).Note that the right branch reaches a leaf node, <strong>and</strong> that this leaf node containsonly permutation YXZ. This means that only permutation YXZ can be the outcomebased on the results of the decisions taken <strong>to</strong> reach this node. In other words,Insertion Sort has “found” the single permutation of the original input that yields asorted list. Likewise, if the second decision resulted in taking the left branch, a thirdcomparison, regardless of the outcome, yields nodes in the decision tree with onlysingle permutations. Again, Insertion Sort has “found” the correct permutation thatyields a sorted list.Any sorting algorithm based on comparisons can be modeled by a decision treein this way, regardless of the size of the input. Thus, all sorting algorithms canbe viewed as algorithms <strong>to</strong> “find” the correct permutation of the input that yieldsa sorted list. Each algorithm based on comparisons can be viewed as proceedingby making branches in the tree based on the results of key comparisons, <strong>and</strong> eachalgorithm can terminate once a node with a single permutation has been reached.How is the worst-case cost of an algorithm expressed by the decision tree? Thedecision tree shows the decisions made by an algorithm for all possible inputs of agiven size. Each path through the tree from the root <strong>to</strong> a leaf is one possible seriesof decisions taken by the algorithm. The depth of the deepest node represents thelongest series of decisions required by the algorithm <strong>to</strong> reach an answer.There are many comparison-based sorting algorithms, <strong>and</strong> each will be modeledby a different decision tree. Some decision trees might be well-balanced, othersmight be unbalanced. Some trees will have more nodes than others (those withmore nodes might be making “unnecessary” comparisons). In fact, a poor sortingalgorithm might have an arbitrarily large number of nodes in its decision tree, withleaves of arbitrary depth. There is no limit <strong>to</strong> how slow the “worst” possible sortingalgorithm could be. However, we are interested here in knowing what the bestsorting algorithm could have as its minimum cost in the worst case. In other words,we would like <strong>to</strong> know what is the smallest depth possible for the deepest node inthe tree for any sorting algorithm.The smallest depth of the deepest node will depend on the number of nodesin the tree. Clearly we would like <strong>to</strong> “push up” the nodes in the tree, but there islimited room at the <strong>to</strong>p. A tree of height 1 can only s<strong>to</strong>re one node (the root); thetree of height 2 can s<strong>to</strong>re three nodes; the tree of height 3 can s<strong>to</strong>re seven nodes,<strong>and</strong> so on.Here are some important facts worth remembering:

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

Saved successfully!

Ooh no, something went wrong!