

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


worksheet 15: Tree Sort (Skip List Version) Name:
A skip List provides a very fast O(log n) execution time for all three of the fundamental Bag operations: addition, contains, and remove. It also maintains values in sorted order. If we add an iterator then we can loop over all the values, in order, in O(n) steps. This makes it a good general purpose container. Here is an application you might not have immediately thought about: sorting. Recall some of the sorting algorithms we have seen. Insertion sort and selection sort are O(n^2 ), although insertion sort can be linear if the input is already nearly sorted. Merge sort and quick sort are faster at O(n log n), although quick sort can be O(n^2 ) in its worst case if the input distribution is particularly bad. Consider the following sorting algorithm: How to sort a vector V Step 1: copy each element from V into a skip list Step 2: copy each element from the skip list back into the vector Assuming the vector has n elements, what is the algorithmic execution time for step 1? What is the algorithmic execution time for step 2? Recall that insertion sort and quick sort are examples of algorithms that can have very different execution times depending upon the distribution of input values. Is the execution time for this algorithm dependent in any way on the input distribution? A sorted vector bag also maintained elements in order. Why would this algorithm not work with that data structure? What would be the resulting algorithmic execution time if you tried to do this? Can you think of any disadvantages of this algorithm?
worksheet 15: Tree Sort (Skip List Version) Name:
The algorithm shown above is traditionally known as tree sort (because it is traditionally performed with AVL trees, rather than skip lists). Complete the following implementation of this algorithm. Note that you have two ways to form a loop. You can use an indexing loop, or an iterator loop. Which is easier for step 1? Which is easier for step 2? void treeSort (EleType [ ] data, int n) { /* sort values in array data */ skipList slst; skipListInit (&slst); }