Programming Practicum
Day 2: Problem Solving with ListsAaron TanNUS School of Computing
2
Contents List as an ADT Review of Day 1 problems Task: Sum of 2 elements Heap and Heapsort
[Programming Practicum, December 2009]
3
List as an ADT (1/2) Abstract Data Type (ADT): data structure +
set of operations Data structure: a collection of related data Some properties: dynamic/static; linear/non-
linear; ordered/unordered; homogeneous/heterogeneous
Some data structures arrays, linked lists, trees, graphs
Some operations insert, delete, modify, search, sort
3[Programming Practicum, December 2009]
4
List as an ADT (2/2)
4[Programming Practicum, December 2009]
Arrays
Linked list
Tree
5
Arrays Array: a list data structure Properties: (usually) static; linear; homogeneous;
quick access to list element Can be multi-dimensional; ordered or unordered Easy to implement and manipulate
5[Programming Practicum, December 2009]
A one-dimensional array A two-dimensional array
6
Day 1 Ex 1: Black and White Balls (1/2) Relative position of black ball w.r.t. its desired
position
6[Programming Practicum, December 2009]
Current position: 3Desired position: 0Difference = 3 Current position: 6
Desired position: 1Difference = 5 Current position: 8
Desired position: 2Difference = 6
Answer: 3 + 5 + 6 = 14
7
Day 1 Ex 1: Black and White Balls (2/2) Rearrangement with neighbor-swaps: 14 swaps Better strategy: find first white ball from left and
first black ball from left, then swap. Repeat.
7[Programming Practicum, December 2009]
1 swap
1 swap
1 swap
Answer: 3 swaps
8
Day 1 Ex 2: Dropping Ball Modelling of problem Using 2D array
Using codes Using 1D array
With sorting
8[Programming Practicum, December 2009]
0 1 2 3 4
Figure 1a.
0 1 2 3 4
Figure 1b.
9
Day 1 Ex 3: Class Schedule Modelling of problem
9[Programming Practicum, December 2009]
200 240
210 23030 60
80 10010 40
200 260
260 280150 180
160 170
Longest lesson?Number of free periods?Maximum number of concurrent lessons?
10
Task: Sum of 2 Elements (1/2) Given a sorted array and a value S, find positions of two
distinct elements whose sum = S. Example:
[Programming Practicum, December 2009]
2 3 5 7 8 10
list
If S = 11, then 3+8 = 11, and answer is 1 and 4. If S = 13, there are two possibilities, 3+10 or 5 + 8. Only one set
of answers is required. If S = 6, there is no solution.
11
Task: Sum of 2 Elements (2/2) Consider this algorithm:
[Programming Practicum, December 2009]
public static void find2Elements (int[] list, int sum) {
for (int i=0; i<list.length; i++) { for (int j=0; j<list.length; j++) { if ((i != j) && (list[i] + list[j] == sum)) System.out.println("Answer: " + i + " and " + j); return; } } } System.out.println("No solution"); return;}
Write a more efficient method.
12
Heap sort (1/11) Basic comparison-sorts:
Bubble sort, selection sort, insertion sort Time complexity: O(n2)
Heap sort Invented by J.R.J. Williams, 1964 Same idea as selection sort, but using a data structure
called heap to perform the main step more quickly Time complexity: O(n lg n)
[Programming Practicum, December 2009]
13
Heap sort (2/11) Heap:
Conceptually, a binary-tree-like structure
Almost complete binary tree All levels are completed filled,
except possibly the lowest level Heap property (or heap order):
Value in a node is larger than values of its children
Hence, root node contains largest value
Also known as max-heap (to distinguish from min-heap)
[Programming Practicum, December 2009]
16
11 9
10 5 6 8
1 2 4
14
Heap sort (3/11) The nice thing about the heap is that we can implement it
using an array, instead of a binary tree. Hence,
[Programming Practicum, December 2009]
16
11 9
10 5 6 8
1 2 4 Note that arr[i] is the parent of arr[2*i+1] and arr[2*i+2].Conversely,the parent of arr[j] (except the root) is arr[(j-1)/2].
16 11 9 10 5 6 8 1 2 4
arr
15
Heap sort (4/11) Heap sort comprises two phases:
Phase 1: Build heap (heapify) Phase 2: Select largest value and adjust heap
Both phases make use of siftDown method siftDown(arr, start, end)
required when arr[start] to arr[end] violates heap order sift value down the heap to restore heap order
[Programming Practicum, December 2009]
5
12 8
10 7 6
start 12
5 8
10 7 6
12
10 8
5 7 6
16
Heap sort (5/11) Phase 1: Build heap (heapify)
[Programming Practicum, December 2009]
5 1 8 10 4 6 9 16 2 11
Before
16 11 9 10 5 6 8 1 2 4
After
heapify(int[] arr) {
for (int i=(arr.length – 2)/2; i>= 0; i--) siftDown(arr, i, arr.length-1);
}
17
Heap sort (6/11)
[Programming Practicum, December 2009]
5
1 8
10 4 6 9
16 2 11
5
1 8
10 11 6 9
16 2 4
heapify(int[] arr) { for (int i=(arr.length – 2)/2; i>= 0; i--) siftDown(arr, i, arr.length-1);}
siftDown(arr, 4, 9) siftDown(arr, 3, 9)
5
1 8
16 11 6 9
10 2 4
siftDown(arr, 2, 9)
5
1 9
16 11 6 8
10 2 4
5 1 8 10 4 6 9 16 2 11
Before
18
Heap sort (7/11)
[Programming Practicum, December 2009]
heapify(int[] arr) { for (int i=(arr.length – 2)/2; i>= 0; i--) siftDown(arr, i, arr.length-1);}
siftDown(arr, 1, 9)
5
1 9
16 11 6 8
10 2 4
…continue from previous slide
5
16 9
10 11 6 8
1 2 4
siftDown(arr, 0, 9)
16
11 9
10 5 6 8
1 2 4
16 11 9 10 5 6 8 1 2 4
After
19
Heap sort (8/11) Phase 2: Select largest value, swap, and adjust heap
[Programming Practicum, December 2009]
for (int last = arr.length – 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1);}
16
11 9
10 5 6 8
1 2 4
4
11 9
10 5 6 8
1 2 16
last
swap siftDown
11
10 9
4 5 6 8
1 2 16
11
10 9
4 5 6 8
1 2 16
last
swap
2
10 9
4 5 6 8
1 11 16
siftDown
10
5 9
4 2 6 8
1 11 16
20
Heap sort (9/11)
[Programming Practicum, December 2009]
swap siftDown
last
10
5 9
4 2 6 8
1 11 16
1
5 9
4 2 6 8
10 11 16
9
5 8
4 2 6 1
10 11 16
last
9
5 8
4 2 6 1
10 11 16
swap
1
5 8
4 2 6 9
10 11 16
siftDown
8
5 6
4 2 1 9
10 11 16
swap siftDown
8
5 6
4 2 1 9
10 11 16 last
1
5 6
4 2 8 9
10 11 16
6
5 1
4 2 8 9
10 11 16
for (int last = arr.length – 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1);}
21
Heap sort (10/11)
[Programming Practicum, December 2009]
swap siftDown
last
6
5 1
4 2 8 9
10 11 16
2
5 1
4 6 8 9
10 11 16
5
4 1
2 6 8 9
10 11 16
last
swap siftDown
5
4 1
2 6 8 9
10 11 16
2
4 1
5 6 8 9
10 11 16
4
2 1
5 6 8 9
10 11 16
swap siftDown
4
2 1
5 6 8 9
10 11 16
last
1
2 4
5 6 8 9
10 11 16
2
1 4
5 6 8 9
10 11 16
for (int last = arr.length – 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1);}
22
Heap sort (11/11)
[Programming Practicum, December 2009]
swap siftDownlast
2
1 4
5 6 8 9
10 11 16
1
2 4
5 6 8 9
10 11 16
1
2 4
5 6 8 9
10 11 16
Time complexity of Heap sort: Phase 1 (heapify): O(n) Phase 2: O(n lg n) (Details of analysis left for CS1102)
Hence, overall: O(n lg n) Implement Heap sort yourself Animation website:
http://www2.hawaii.edu/~copley/665/HSApplet.html
for (int last = arr.length – 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1);}
23
THE END