Analysis of Algorithim
All the sorting algorithms we have seen so far
are comparison sorts: only use comparisons to
determine the relative order of elements.
The best worst-case running time that we’ve
seen for comparison sorting is O(nlgn).
Is O(nlgn) the best we can do?
Decision trees can help us answer this
• The basic idea is to expand the problem into a
complete decision tree
• The leaves of the tree are where the algorithm
terminates (i.e. our problem has been decided)
– there are therefore as many leaves as possible
• The height and branching factor of the tree give
us an indication of the complexity of the problem
– for simplicity we restrict ourselves to binary trees -
operations such as comparison are binary in any case
Decision Tree for Sorting 3
y n y n
y n y n
A<CB CA<B BA<C BC<A
A Shorter Tree?
• Any sorting by comparison algorithm must be a
decision tree of some kind, although it might be
much bigger if the algorithm is inefficient
– e.g. the same comparison could appear in many
• The height of this particular tree is three
• Can we make a shorter tree which still solves
– no we can’t. A binary tree with only two levels has at
most four leaves, but there are six possible
permutations of three items
A decision tree can model the execution of any
• One tree for each input size n.
• View the algorithm as splitting whenever it
compares two elements.
• The tree contains the comparisons along all
possible instruction traces.
• The running time of the algorithm =the length of
the path taken.
• Worst-case running time =height of tree.
Lower bound for decision-tree