Presentation is loading. Please wait.

Presentation is loading. Please wait.

2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.

Similar presentations


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

1 2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW COMP 103 John Lewis Review 2

2 2  Help desk: Thursday 13 and 20 in CO254, 3pm (Michael and Roma’s office)  Also this week at regular time/place (3pm Co242a)

3 3 Exam  Answer the easy questions first  Look at previous year’s exams  Solidify your knowledge by comparing things  List vs Array? Queue vs list? List vs BST? Array vs BST? ….  Be able to describe to your friend: the algorithm, its complexity, its advantage, disadvantage  Other similar courses online, wikipedia, etc.

4 4 Implement one data structure in terms of others  Ex: priority queue using heap  Queue using linked list  Stack using linked list

5 5  Binary search – BST

6 6 Sorts  Heap, Tree, Merge, Quick…  Worst case time?  Memory?

7 7 Analysing Sorting Algorithms  Efficiency  What is the (worst-case) order of the algorithm ?  Is the average case much faster than worst-case ?  Requirements on Data  Does the algorithm need random-access data?  Does it need anything more than “compare” and “swap” ?  Space Usage  Can the algorithm sort in-place, or does it need extra space ?  Stability  Is the algorithm “stable” (will it ever reverse the order of equivalent items?)  Performance on Nearly Sorted Data  Is the algorithm faster when given sorted (or nearly sorted) data ? (All items close to where they should be, or only a few out of order.) 7

8 8 Design decisions...  We could sort Lists ⇒ general and flexible but efficiency depends on how the List is implemented or just sort Arrays ⇒ less general efficiency is well defined NB: method toArray() converts any Collection to an array  We could require items to be Comparable i.e. any item can call compareTo(otherObj)...on another ("natural ordering") OR provide a Comparator i.e. the sorter can call compare(obj1, obj2)...on any two items. We will sort an Array, using a Comparator: public void …Sort(E[] data, int size, Comparator comp) number of items 8 comparator array

9 9

10 10 0123456789 11 Selecting Sorts  Selection Sort (slow)  HeapSort (fast) 01234567891011 search for minimum here 10

11 11  Insertion Sort (slow)  Shell Sort (pretty fast)  Merge Sort (fast) (Divide and Conquer) 01234567891011 Inserting Sorts 01356781011 249 How would you describe this in words?

12 12

13 13 HeapSort Given an array of n items: (a) re-order them into a Heap from 0 to n-1: for i = (n-1)/2 down to 0 pushdown(i) 0137894265 35 2619 147 923 13 1 4 now we have a heap! "heapify"

14 14 HeapSort Given an array of n items: (a) re-order them into a Heap from 0 to n-1: for i = (n-1)/2 down to 0 pushdown(i) (b) for pos = n -1 down to 0 treat 0 ⋯ pos-1 as a heap poll item and place into position pos etc etc ! 0137894265 352619147923131 4 Sorted → in-place dequeueing... "heapify" and so on!

15 15 HeapSort (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-1)/2; I >= 0; i--) pushDown(i, data, size, comp); while (size > 0) { size--; swap(data, size, 0); pushDown(0, data, size, comp); } "heapify" in-place dequeueing

16 16 recursion Y X B X B M M A A Prints: X Y B A M 1. 2. 3.

17 17 recursion Y X B X B M M A A Prints: X Y B A M 1. 2. 3.

18 18 Programming review  == versus equals()  Assignment: copy versus reference ArrayList a = new ArrayList (); a.add(1); a.add(2); a.add(3); a.add(4); ArrayList b = a; b.set(1,100); printArr(a); printArr(b);


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

Similar presentations


Ads by Google