Programming Practicum Day 2 Problem Solving with Lists
Programming Practicum Day 2: Problem Solving with Lists Aaron Tan NUS School of Computing
Contents n n List as an ADT Review of Day 1 problems Task: Sum of 2 elements Heap and Heapsort [Programming Practicum, December 2009] 2
List as an ADT (1/2) n n Abstract Data Type (ADT): data structure + set of operations Data structure: a collection of related data Some properties: dynamic/static; linear/nonlinear; ordered/unordered; homogeneous/heterogeneous Some data structures q n arrays, linked lists, trees, graphs Some operations q insert, delete, modify, search, sort [Programming Practicum, December 2009] 3
List as an ADT (2/2) Arrays Tree Linked list [Programming Practicum, December 2009] 4
Arrays n n 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 A one-dimensional array [Programming Practicum, December 2009] A two-dimensional array 5
Day 1 Ex 1: Black and White Balls (1/2) n Relative position of black ball w. r. t. its desired position Current position: 3 Desired position: 0 Difference = 3 Current position: 6 Desired position: 1 Difference = 5 Current position: 8 Desired position: 2 Difference = 6 Answer: 3 + 5 + 6 = 14 [Programming Practicum, December 2009] 6
Day 1 Ex 1: Black and White Balls (2/2) n Rearrangement with neighbor-swaps: 14 swaps n Better strategy: find first white ball from left and first black ball from left, then swap. Repeat. 1 swap Answer: 3 swaps [Programming Practicum, December 2009] 7
Day 1 Ex 2: Dropping Ball n n 1 2 3 4 Modelling of problem Using 2 D array q n 0 Using codes Using 1 D array q With sorting Figure 1 a. 0 1 2 Figure 1 b. [Programming Practicum, December 2009] 8
Day 1 Ex 3: Class Schedule n Modelling of problem 200 240 210 30 60 80 10 230 100 40 200 260 150 280 160 170 Longest lesson? Number of free periods? Maximum number of concurrent lessons? [Programming Practicum, December 2009] 9
Task: Sum of 2 Elements (1/2) n Given a sorted array and a value S, find positions of two distinct elements whose sum = S. q Example: list 2 q q q 3 5 7 8 10 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. [Programming Practicum, December 2009] 10
Task: Sum of 2 Elements (2/2) n Consider this algorithm: public static void find 2 Elements (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; } n Write a more efficient method. [Programming Practicum, December 2009] 11
Heap sort (1/11) n Basic comparison-sorts: q q n Bubble sort, selection sort, insertion sort Time complexity: O(n 2) Heap sort q q q 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] 12
Heap sort (2/11) n Heap: q q Conceptually, a binary-tree-like structure Almost complete binary tree n q All levels are completed filled, except possibly the lowest level 11 9 10 1 5 2 6 8 4 Heap property (or heap order): n n q 16 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] 13
Heap sort (3/11) n n The nice thing about the heap is that we can implement it using an array, instead of a binary tree. Hence, 16 11 arr 9 16 10 1 5 2 6 4 [Programming Practicum, December 2009] 11 9 10 5 6 8 1 2 4 8 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]. 14
Heap sort (4/11) n Heap sort comprises two phases: q q n n Phase 1: Build heap (heapify) Phase 2: Select largest value and adjust heap Both phases make use of sift. Down method sift. Down(arr, start, end) q q required when arr[start] to arr[end] violates heap order sift value down the heap to restore heap order start 5 12 12 10 8 7 6 [Programming Practicum, December 2009] 12 5 10 8 7 6 10 5 8 7 6 15
Heap sort (5/11) n Phase 1: Build heap (heapify) Before 5 1 8 10 4 6 9 16 2 11 11 9 10 5 6 8 1 2 4 After 16 heapify(int[] arr) { for (int i=(arr. length – 2)/2; i>= 0; i--) sift. Down(arr, i, arr. length-1); } [Programming Practicum, December 2009] 16
Heap sort (6/11) heapify(int[] arr) { for (int i=(arr. length – 2)/2; i>= 0; i--) sift. Down(arr, i, arr. length-1); } Before 5 1 8 10 4 6 9 16 2 11 5 5 1 8 4 10 16 2 1 sift. Down(arr, 4, 9) 9 6 8 11 10 11 16 2 sift. Down(arr, 3, 9) 9 6 4 5 5 1 8 1 9 sift. Down(arr, 2, 9) 16 10 11 2 6 4 [Programming Practicum, December 2009] 9 16 10 11 2 6 8 4 17
Heap sort (7/11) heapify(int[] arr) { for (int i=(arr. length – 2)/2; i>= 0; i--) sift. Down(arr, i, arr. length-1); } …continue from previous slide 5 5 1 9 16 9 sift. Down(arr, 1, 9) 16 10 11 2 sift. Down(arr, 0, 9) 8 6 10 4 1 11 2 8 6 4 16 11 9 After 10 1 5 2 6 8 16 11 9 10 5 6 8 1 2 4 4 [Programming Practicum, December 2009] 18
Heap sort (8/11) n Phase 2: Select largest value, swap, and adjust heap for (int last = arr. length – 1; last>0; last--) { swap arr[0] with arr[last] sift. Down(arr, 0, last-1); } 4 16 11 9 10 1 5 2 6 11 swap 8 9 10 4 11 1 5 2 6 10 sift. Down 8 9 5 4 1 16 2 6 8 16 last 11 2 10 9 5 4 1 2 6 10 swap 8 9 5 4 1 16 10 11 16 6 5 sift. Down 8 9 4 1 2 11 6 8 16 last [Programming Practicum, December 2009] 19
Heap sort (9/11) 10 1 5 9 4 1 2 6 9 5 swap 9 4 8 16 11 for (int last = arr. length – 1; last>0; last--) { swap arr[0] with arr[last] sift. Down(arr, 0, last-1); } 10 2 6 8 4 8 16 11 5 sift. Down 10 2 1 6 16 11 last 9 1 5 8 4 10 6 2 11 5 swap 1 8 4 last 16 10 11 6 16 5 sift. Down 9 6 4 16 10 2 11 1 9 last [Programming Practicum, December 2009] 6 4 10 2 11 16 9 6 5 swap 1 16 1 5 2 6 2 8 4 8 8 5 sift. Down 9 1 4 10 2 11 8 9 16 20
for (int last = arr. length – 1; last>0; last--) { swap arr[0] with arr[last] sift. Down(arr, 0, last-1); } Heap sort (10/11) 6 2 5 1 4 10 2 8 5 swap 9 1 4 last 16 11 10 6 10 11 1 8 9 5 10 6 11 1 8 2 sift. Down 9 8 9 10 6 11 16 [Programming Practicum, December 2009] 10 6 11 16 8 9 16 2 4 5 9 1 5 16 2 swap 8 16 11 1 last 5 6 4 1 4 2 2 10 4 swap last 16 9 1 2 4 6 8 4 sift. Down 16 11 5 2 5 8 1 sift. Down 9 4 5 10 6 11 8 9 16 21
for (int last = arr. length – 1; last>0; last--) { swap arr[0] with arr[last] sift. Down(arr, 0, last-1); } Heap sort (11/11) 2 1 6 11 n q n 9 4 5 10 6 11 8 16 2 sift. Down 9 4 5 10 6 11 8 9 16 Time complexity of Heap sort: q n 8 1 2 swap 16 q n 4 last 5 10 1 Phase 1 (heapify): O(n) Phase 2: O(n lg n) (Details of analysis left for CS 1102) Hence, overall: O(n lg n) Implement Heap sort yourself Animation website: http: //www 2. hawaii. edu/~copley/665/HSApplet. html [Programming Practicum, December 2009] 22
THE END 23
- Slides: 23