2011-t2 lecture 21 school of engineering and computer science, victoria university of wellington ...
TRANSCRIPT
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria
University of Wellington
Marcus Frean, Lindsay Groves, and Peter Andreae, VUW
CO
MP 1
03
John Lewis
Review 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
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
Implement one data structure in terms of others
Ex: priority queue using heap Queue using linked list Stack using linked list
5
Binary search – BST
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
8Design 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 Comparatori.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<E> comp)number of items
8
comparatorarray
9
10
0 1 2 3 4 5 6 7 8 9 10 11
Selecting Sorts
Selection Sort (slow) HeapSort (fast)
0 1 2 3 4 5 6 7 8 9 10 11
search for minimum here10
11
Insertion Sort (slow) Shell Sort (pretty fast) Merge Sort (fast)
(Divide and Conquer)
0 1 2 3 4 5 6 7 8 9 10 11
Inserting Sorts
0 1 3 5 6 7 8 10 112 4 9
11
How would you describe this in words?
12
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)
0 1 3 7 8 942 65
3526 191479 2313 14
now we have a heap!
"heapify"
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 heappoll item and place into position pos
etc etc !
0 1 3 7 8 942 65
35 2619 14 792313 14
Sorted →
in-place dequeueing...
"heapify"
and so on!
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<E> 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
17recursion
YX
B
X B
M
M
A
A
Prints: X Y B A M
1.2.3.
18
Programming review == versus equals()
Assignment: copy versus reference
ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); a.add(2); a.add(3); a.add(4); ArrayList<Integer> b = a; b.set(1,100);
printArr(a); printArr(b);