Presentation is loading. Please wait.

Presentation is loading. Please wait.

2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.

Similar presentations


Presentation on theme: "2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas."— Presentation transcript:

1 2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas Kuehne, VUW COMP 103 Marcus Frean HeapSort

2 2 RECAP-TODAY RECAP  Priority Queue, POTs, Heaps  Priority Queue  another fast sorting algorithm! TODAY  HeapSort – the PriorityQueue sort, done in an array  Introduction to Hashing (maybe)

3 3 recap: Heap  “Heap” = a complete POT, implemented in an array  Bottom right node is last element used  We can compute the index of parent and children of a node:  the children of nodei are at(2i+1) and (2i+2)  the parent of nodei is at (i-1)/2  note: no gaps! Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat 4 0137894265 Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat 4

4 4 Heapsort: In-Place Sorting  Use an array-based Heap  in-place sorting algorithm! 1. turn the array into a heap 2. “remove” top element, and restore heap property again 3. repeat step 2. n-1 times in-place dequeueing 0137894265 352619147923131 4 Sorted → and so on!

5 5 Heapsort: In-Place Sorting How to turn the array into a heap? Heapify for i = lastParent down to 0 sinkdown(i) heap property installed heap property installed (n-2)/2 0137894265 35 2619 147 923 13 1 4

6 6 HeapSort: Algorithm (a) Turn data into a heap (b) Repeatedly swap root with last item and push down public void heapSort(E[] data, int size, Comparator comp) { for (int i = (size-2)/2; i >= 0; i--) sinkDown(i, data, size, comp); while (size > 0) { size--; swap(data, size, 0); sinkDown(0, data, size, comp); } } "heapify" in-place dequeueing

7 7 Cost of “heapify” (n/2 log (n+1) -1)  (log(n+1)-1) n/8  2 n/4  1 n/2  0 Cost = n [1/4 + 2/8 + 3/16 + 4/32 + ⋯ (log(n+1)-1)/n] swaps = ⋮ swaps =

8 8 Cost of “heapify” Cost= n  [ 1/4 + 2/8 + 3/16 + 4/32 + ⋯ ] = n  [ (1/4 + 1/8 +1/16 + 1/32 + ⋯ ) + ( 1/8 +1/16 + 1/32 + ⋯ ) + ( 1/16 + 1/32 + ⋯ ) + ⋮ ⋮ ≈ n  [ 1/2 + 1/4 + 1/8 + ⋯ ] ≈ n  [1] = n swaps  We can turn an unordered list into a heap in linear time!!! 1 st row 2 nd row 3 rd row 1 st row 2 nd row 3 rd row

9 9 HeapSort: Summary  Cost of heapify = O(n)  n  Cost of remove= O(n log n)  Total cost = O(n log n)  True for worst-case and average case!  unlike QuickSort and TreeSort  Can be done in-place  Unlike MergeSort, doesn’t need extra memory to be fast  Not stable 

10 10 NEW TOPIC: Sets with O(1) operations? ✔ We need a way to compute an index for an object: add(“2001 – A Space Odyssey”) “Hashing”: compute the “hash code” of an object 0123456789581N ✔✗✔✔✗✗✗✗✗✗✗ ⋯⋯ ✗ Hash function 581 “ 2001 – A Space Odyssey ”

11 11 O(1) Sets with big values?  But there are too many possible film titles!  Suppose the hash function always produces a number between 0 and 1000 ⇒ some film titles must end up with the same number! ⇒ “Collision” 0123456789581N ✔✗✔✔✗✗✗✗✗✗✗ ⋯⋯ ✔✔ HASH “ Gravity ” “ 2001 – A Space Odyssey ” HASH

12 12 Detecting collisions  Store the item in the array, instead of a boolean  Questions 1. How to choose hash function that minimises collisions? 2. How to manage collisions when they occur? 0123456789581N ⋯⋯ “ Gravity ” “ 2001 – A Space Odyssey ” HASH

13 13 Computing Hash Codes Wish list Summary for HashCode Function  Should produce an integer  Should distribute the hash codes evenly through the range minimises collisions  Should be fast to compute  Should take account of all components of the object  Must be consistent with equals() two items that are equal must have the same hash value Can we avoid clashes altogether? That would be perfect!  perfect hash function

14 14 A Simple Hash Function for Strings  We could add up the codes of all the characters: private int hash(String value) { int hashCode = 0; for (int i = 0; i < value.length(); i++) hashCode += value.charAt(i); return hashCode; } Why is this not very good?

15 15 Example: Hashing course codes 418 ← DEAF101 419 ← DEAF102 DEAF201 ⋮ 429 ← BBSC201 MDIA101 430 ← ECHI410 MDIA102 MDIA201 431 ← ECHI303 JAPA111 JAPA201 MDIA202 MDIA220 MDIA301 432 ← ARCH101 ASIA101 BBSC231 BBSC303 BBSC321 CHEM201 ECHI403 ECHI412 JAPA112 JAPA211 JAPA301 MDIA203 MDIA302 MDIA320 ⋮ 450 ← ANTH412 ARCH389 ARTH111 BIOL228 BIOL327 BIOL372 CHEM489 COML304 COML403 COML421 COMP102 COMP201 CRIM313 CRIM421 DESN215 DESN233 ECON328 ECON409 ECON418 ECON508 EDUC449 EDUC458 EDUC548 EDUC557 ENGL228 ENGL408 ENGL426 ENGL435 ENGL444 ENGL453 FREN124 FREN331 FREN403 FREN412 GEOL362 GEOL407 GERM214 GERM403 GERM412 INFO213 INFO312 INFO402 ITAL206 ITAL215 LALS501 LATI404 LING224 LING323 LING404 MAOR102 MARK304 MARK403 MATH206 MATH314 MATH323 MATH431 MOFI403 PHIL104 PHIL203 PHIL302 PHIL320 PHIL401 PHIL410 RELI321 RELI411 SAMO101 ⋮ a lot of collisions!

16 16 Better Hash Functions  Make the contribution of each character depend on its position: private int hash(String course) { int k = 257; int hashCode = 0; for (int i = 0; i < course.length(); i ++ ) hashCode = hashCode * k + course.charAt(i); return hashCode; } hashCode(s) = k 6 x s 0 + k 5 x s 1 + k 4 x s 2 + k 3 x s 3 + k 2 x s 4 + k 1 x s 5 + s 6 (it is best to use a prime number for the constant k)


Download ppt "2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas."

Similar presentations


Ads by Google