# day18 - COP 3503 Computer Science II CLASS NOTES DAY#18...

This preview shows pages 1–3. Sign up to view the full content.

COP 3503 – Computer Science II CLASS NOTES - DAY #18 Shell Sort Algorithms From Data Structures and Problem Solving using Java , Mark Weiss: void shellsort (int[] a) { for (int gap = a.length / 2; gap > 0;gap = gap == 2 ? 1 : (int) ( gap / 2 ) ) for(int i = gap; i < a.length; i++ ){ int tmp = a[ i ]; int j = i; for( ; j >= gap & tmp a[ j ] = a[ j -gap ]; a[ j ] = tmp; } } From Data Structures, Algorithms, and Applications in Java, Sartaj Sahni: public class ShellSort { /** sort the elements a[0 : a.length - 1] using * the shaker sort method */ public static void shellSort(Comparable [] a) { int incr = a.length / 2; // initial increment while (incr >= 1) {// insertion sort all sequences spaced by incr for (int i = incr; i < a.length; i++) {// insert a[i] into a[i - incr], a[i - 2*incr], . .. Comparable insertElement = a[i]; int j; for (j = i - incr; j >= 0 & insertElement.compareTo(a[j]) j -= incr) a[j + incr] = a[j]; a[j + incr] = insertElement; } // reduce increment by a factor of 2.2 if (incr == 2) incr = 1; // last increment must be 1 else incr = (int) (incr / 2.2); } } } Day 18 - 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
As we have discussed within the context of the binary search tree, the performance of the algorithms which utilize the BST are dependent upon the height of the tree. We have looked at the DSW algorithm to balance a binary tree. In this section of the notes we will look at binary trees which in addition to the ordering properties which ensure that the tree is a search tree, there will be structure properties imposed on the tree which ensures that the tree is balanced. So all of the binary search tree variants that are discussed in this section of the notes are self-balancing search trees. AVL Trees The AVL tree (named after its discoverers, Adelson-Velskii and Landis, but originally called an admissible tree ) was the first balanced binary search tree. Any search tree which is self-balancing must use balance conditions which are easy to maintain and ensure that the height of the tree is O(log n), where n is the number of nodes in the tree. The simplest idea is to require that the left and right subtrees have the same height. Using the recursive definition of the binary search tree implies that this condition be applied to all the nodes in the tree, since every node is considered the root of some subtree. While this simple requirement will ensure that the height of the tree is logarithmic in terms of n , it is too restrictive because inserting new key values while maintaining overall balance is too difficult (i.e., too costly in terms of time). Therefore, the definition of the AVL tree uses a slightly different interpretation of balance, one that is weaker than total balance between all left and right subtrees, yet is still strong enough to ensure logarithmic height of the tree. Notice that the DSW algorithm (see Day 22 notes) balances trees using the same
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 06/12/2011.

### Page1 / 13

day18 - COP 3503 Computer Science II CLASS NOTES DAY#18...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online