Title: Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M
1Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M
- Big O Notation
- We say that a function is
if for some
constant c, when n is large. - For example, is
since for n
gt 3. - This is described by saying that
is on the order of . - Big O notation calls attention to the part of a
function that grows the fastest, so gives a
simple estimate of how many steps are required
for an algorithm to run.
2Binary Testing Tree
Compare things (objects, subsets, lists,
whatever) in some way with two outcomes.
Keep comparing
Eventually arrive at one of the n! possible orders
Since there are n! leaves on this tree, by
Theorem 3 in section 3.1, the tree has height
. Thus,
whatever binary comparison technique is used, the
worst case will require at least
comparisons.
3Bubble Sort
- For m from 2 to n, do
- For j from n (step 1) to m do
- If Aj lt Aj-1, then interchange Aj and Aj-1.
4Complexity of the Bubble Sort
- When m 2, j goes from n to 2, so you have to do
n 1 comparisons. - When m 3, j goes from n to 3, so you have to do
n 2 comparisons, and so on. - Thus, the total number of comparisons is
- Thus, the Bubble sort takes
comparisons, which is a lot more than the - theoretical bound of .
5Merge SortSubdivision Tree
First divide the set repeatedly, roughly in half,
until only single elements are left at the
leaves.
6Merge SortMerging Tree
then merge the sequences in order.
4
7
0
5
4 7
6
1
0 5
2
8
3
9
1 4 7
2 6
0 5 9
3 8
Note This tree is drawn upside down, so that
the root is at the bottom. Thus, the set 0, 5
is at level 3
0 2 5 6 9
1 3 4 7 8
1 2 3 4 5 6 7 8 9
7Complexity of MergingA Simplifying Assumption
- Assume our set has n 2r elements. This means
that - We always divide exactly in half in the
subdivision tree. - There are r levels
- All the leaves in both the subdivision and the
merging tree are on level r. - There are 2k vertices on level k
- The sets on level k all have 2r-k elements in them
n 23
8Complexity of MergingCounting the Comparisons
- At each vertex on level k of the merging tree we
merge the two children sets each of size 2r-k-1. - Merging two lists of length L into a single list
of length 2L requires 2L-1 comparisons (class
exercise). - For each vertex on level k, this merging takes 2
(2r-k-1) 1 2r-k 1 comparisons - There are 2k vertices on level k, for a total of
2k (2r-k 1 ) 2r - 2k comparisons on each
level. - Since we must do this on each level, the total
number of comparisons for a Merge Sort is - Since n 2r, this becomes
. - (Still need to account for the subdivision
preprocessing, but this isnt bad) - Thus Merge Sort is and
achieves the theoretical bound of a binary search.
9QUIK Sort
Use the first element in the list to partition
the list, then put it at the end of the left hand
child list. Worst case, , on average
5 0 9 2 6 47 1 3 8
9 6 7 8
0 2 4 1 3 5
6 7 8 9
0
2 4 1 3 5
7 8 9
6
1 2
4 3 5
8 9
3 4
7
5
2
1
8
4
9
3
10Heap Sort
- A heap is a (nearly) binary tree so that a parent
is always bigger than its children (root is
biggest of all). - Put the root at the beginning of a list, then
move up the biggest grandchildren.
Preprocessing Need to first construct the heap
(homework)
11Class Exercises
- Prove that it takes at most n 1 comparisons to
merge two sorted lists into a single sorted list
of length n. - Sort 4, 5, 2, 3, 0, 1 using each of a Bubble,
Merge, QUIK, and Heap Sort.