Docsity

Tree Sort Algorithm: Execution Time and Comparison with Quick Sort and Merge Sort, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
worksheet 15: Tree Sort (Skip List Version) Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 15: Tree Sort (Skip List Version)
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(n2), 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(n2) 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?
pf2

Partial preview of the text

Download Tree Sort Algorithm: Execution Time and Comparison with Quick Sort and Merge Sort and more Assignments Data Structures and Algorithms in PDF only on Docsity!

worksheet 15: Tree Sort (Skip List Version) Name:

An Active Learning Approach to Data Structures using C 1

Worksheet 15: Tree Sort (Skip List Version)

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:

An Active Learning Approach to Data Structures using C 2

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); }

On Your Own:

  1. A Sorting algorithm is said to be stable if the relative order of two equal elements in the input sequence is preserved in the output. Is the tree sort algorithm stable? To determine this, simulate the execution of the algorithm on the four-element vector 2 1 , 1, 3, 2 2. Track carefully the relative positions of the two 2 values. Is it possible for them to change places during the sorting process?
  2. Experimentally compare the execution time of tree sort to that of quick sort and merge sort. Are they comparable?