Tree Sort Algorithm using Skip List and AVL Tree in C, Assignments of Data Structures and Algorithms

This worksheet introduces the tree sort algorithm, which utilizes a skip list or avl tree for efficient sorting of an array. The algorithm involves copying each element from the array into the data structure, maintaining the sorted order, and then copying the sorted elements back into the array. The advantages of using this approach, including its o(log n) execution time for fundamental bag operations and the ability to maintain sorted order. It also explores the execution time for step 1 and step 2, and discusses the impact of input distribution and the use of a sorted dynamic array bag. The document concludes by asking the reader to consider disadvantages of this algorithm.

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-m17
koofers-user-m17 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
worksheet 32: Tree Sort (Skip List Version) Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 32: Tree Sort
In Preparation: Read chapter 10 on the Tree data type.
Both the Skip List and the AVL tree provide a very fast O(log n) execution time for all
three of the fundamental Bag operations: addition, contains, and remove. They 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 them 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 array A
Step 1: copy each element from A into a skip list (or AVL tree)
Step 2: copy each element from the skip list back into the array
Assuming the array 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 dynamic array 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 using Skip List and AVL Tree in C and more Assignments Data Structures and Algorithms in PDF only on Docsity!

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

An Active Learning Approach to Data Structures using C 1

Worksheet 32: Tree Sort

In Preparation : Read chapter 10 on the Tree data type. Both the Skip List and the AVL tree provide a very fast O(log n) execution time for all three of the fundamental Bag operations: addition, contains, and remove. They 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 them 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 array A Step 1: copy each element from A into a skip list (or AVL tree) Step 2: copy each element from the skip list back into the array Assuming the array 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 dynamic array 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 32: Tree Sort (Skip List Version) Name:

An Active Learning Approach to Data Structures using C 2

The algorithm shown above is known as tree sort (because it is traditionally performed with AVL trees). 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 */ AVLtree tre; AVLtreeInit (&tre); }