Presentation is loading. Please wait.

Presentation is loading. Please wait.

Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.

Similar presentations


Presentation on theme: "Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010."— Presentation transcript:

1 Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010

2 Outline Hash Table Recursion Sorting

3 A basic problem We have to store some records and perform the following: add new record delete record search a record by key Find a way to do these efficiently!

4 Array as table : 0033333 : 0012345 0000000 : : betty : andy : : 90 : 81.5 : namescore 0056789 david56.8 : 9908080 : : : bill : : : 49 : : 9999999 One ‘stupid’ way is to store the records in a huge array (index 0..9999999). The index is used as the student id, i.e. the record of the student with studid 0012345 is stored at A[12345]

5 Array as table Store the records in a huge array where the index corresponds to the key add - very fast O(1) delete - very fast O(1) search - very fast O(1) But it wastes a lot of memory! Not feasible.

6 Hash function function Hash(key: KeyType): integer; Imagine that we have such a magic function Hash. It maps the key (studID) of the 1000 records into the integers 0..999, one to one. No two different keys maps to the same number. H(‘0012345’) = 134 H(‘0033333’) = 67 H(‘0056789’) = 764 … H(‘9908080’) = 3

7 Hash Table : betty : bill : : 90 : 49 : namescore andy81.5 : : david : : : 56.8 : : 0033333 : 9908080 : 0012345 : : 0056789 : 3 67 0 764 999 134 To store a record, we compute Hash(stud_id) for the record and store it at the location Hash(stud_id) of the array. To search for a student, we only need to peek at the location Hash(target stud_id).

8 Division Method Certain values of m may not be good: When m = 2 p then h (k) is the p lower-order bits of the key Good values for m are prime numbers which are not close to exact powers of 2. For example, if you want to store 2000 elements then m=701 (m = hash table length) yields a hash function: h (k) = k mod m h (key) = k mod 701

9 Collision For most cases, we cannot avoid collision Collision resolution - how to handle when two different keys map to the same index H(‘0012345’) = 134 H(‘0033333’) = 67 H(‘0056789’) = 764 … H(‘9903030’) = 3 H(‘9908080’) = 3

10 Chained Hash Table 2 4 1 0 3 nil 5 : HASHMAX Key: 9903030 name: tom score: 73 One way to handle collision is to store the collided records in a linked list. The array now stores pointers to such lists. If no key maps to a certain hash value, that array entry points to nil.

11 Open Address approach Linear probing: Given auxiliary hash function h, the probe sequence starts at slot h(k) and continues sequentially through the table, wrapping after slot m − 1 to slot 0. Given key k and probe number i (0 ≤ i < m), h(k, i ) = (h(k) + i ) mod m. Quadratic probing: As in linear probing, the probe sequence starts at h(k). Unlike linear probing, it examines cells 1,4,9, and so on, away from the original probe point: h(k, i ) = (h(k) + c 1 i + c 2 i 2 ) mod m (if c1=0, c2=1, it’s the example given by book) Double hashing: Use two auxiliary hash functions, h 1 and h 2. h 1 gives the initial probe, and h 2 gives the remaining probes: h(k, i ) = (h 1 (k) + ih 2 (k)) mod m.

12 Outline Hash Table Recursion Sorting

13 General format for Many Recursive Functions if (some easily-solved condition) // base case solution statement else // general case recursive function call

14 When a function is called... a transfer of control occurs from the calling block to the code of the function--it is necessary that there be a return to the correct place in the calling block after the function code is executed; this correct place is called the return address when any function is called, the run-time stack is used--on this stack is placed an activation record for the function call

15 int Func ( /* in */ int a, /* in */ int b ) { int result; if ( b == 0 ) // base case result = 0; else if ( b > 0 ) // first general case result = a + Func ( a, b - 1 ) ) ; // instruction 50 return result; } A recursive function

16 FCTVAL ? result ? b 2 a 5 Return Address 100 Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100 original call at instruction 100 pushes on this record for Func(5,2)

17 FCTVAL 0 result 0 b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,0) is popped first with its FCTVAL record for Func(5,2) record for Func(5,1) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

18 Too much recursion Can Be Dangerous Fibonacci numbers. Long fib (int n) { If (n <=1) return n; Else return fib(n-1) + fib(n-2); }

19 Too much recursion Can Be Dangerous

20 Recursive Traversal Implementation Void PrintInorder (root) if root != null PrintInorder(root->left); print(root->data); PrintInorder(root->right); endif; Void PrintInorder (root) if root != null PrintInorder(root->left); print(root->data); PrintInorder(root->right); endif; The difference is the order of the three statements in the ‘IF’. 1 23 456 preorder : 1 2 4 5 3 6 inorder : 4 2 5 1 3 6 postorder : 4 5 2 6 3 1 preorder : 1 2 4 5 3 6 inorder : 4 2 5 1 3 6 postorder : 4 5 2 6 3 1 Void PrintPreorder (root) if root != null print(root->data); PrintPreorder(root->left); PrintPreorder(root->right); endif; Void PrintPreorder (root) if root != null print(root->data); PrintPreorder(root->left); PrintPreorder(root->right); endif; Void PrintPostorder (root) if root != null PrintPostorder(root->left); PrintPostorder(root->right); print(root->data); endif; Void PrintPostorder (root) if root != null PrintPostorder(root->left); PrintPostorder(root->right); print(root->data); endif;

21 Outline Hash Table Recursion Sorting

22 What’s Binary Heap The Binary Heap supports the insertion of new items and delete of MIN item in logarithmic worst-case time. It uses only an array to implement. (Instead of linked list) It is the classic method used to implement priority queues

23 Structure Property A COMPLETE BINARY TREE is a tree that complete filled.

24 Basic Operations of Binary Heap Insert operation Delete operation The buildHeap operation can be done in linear time by applying a percolate down routine to nodes in reverse order

25 Insertion Sort: Code template void insertionSort( vector & a ) { for( int p = 1; p < a.size( ); p++ ) { Comparable tmp = a[ p ]; int j; for( j = p; j > 0 && tmp < a[ j - 1 ]; j-- ) a[ j ] = a[ j - 1 ]; a[ j ] = tmp; } Fixed n-1 iterations Worst case i-1 comparisons Move current key to right Insert the new key to its proper position Searching for the proper position for the new key Moved

26 Example of shell sort original81941196123517952858417515 5-sort 35171128124175159658819495 3-sort 28121135154158179475819695 1-sort 11121517283541587581949596

27 27 Binary Merge Sort Merge first one-element "subfile" of F1 with first one-element subfile of F2 Gives a sorted two-element subfile of F Continue with rest of one-element subfiles

28 Quicksort Choose some element called a pivot Perform a sequence of exchanges so that All elements that are less than this pivot are to its left and All elements that are greater than the pivot are to its right.

29 Quicksort Given to sort: 75, 70, 65, 84, 98, 78, 100, 93, 55, 61, 81, 68 Select, arbitrarily, the first element, 75, as pivot. Search from right for elements <= 75, stop at first element <75 And then search from left for elements > 75, starting from pivot itself, stop at first element >=75 Swap these two elements, and then repeat this process until Right and Left point at the same location

30 Quicksort Example Need to sort (independently): 55, 70, 65, 68, 61 and 100, 93, 78, 98, 81, 84 Let pivot be 55, look from each end for values larger/smaller than 55, swap Same for 2 nd list, pivot is 100 Sort the resulting sublists in the same manner until sublist is trivial (size 0 or 1) View quicksort() recursive functionquicksort()

31 Quicksort Note visual example of a quicksort on an array etc. …

32 Radix Sort Approach 1. Decompose key C into components C1, C2, … Cd Component d is least significant, Each component has values over range 0..k

33


Download ppt "Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010."

Similar presentations


Ads by Google