1 / 23

Programming Practicum

Programming Practicum. Day 2: Problem Solving with Lists Aaron Tan NUS School of Computing. Contents. List as an ADT Review of Day 1 problems Task: Sum of 2 elements Heap and Heapsort. List as an ADT (1/2). Abstract Data Type (ADT): data structure + set of operations

pierce
Download Presentation

Programming Practicum

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Practicum Day 2: Problem Solving with Lists Aaron Tan NUS 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 [Programming Practicum, December 2009] 3

  4. List as an ADT (2/2) Arrays Tree Linked list [Programming Practicum, December 2009] 4

  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 A one-dimensional array A two-dimensional array [Programming Practicum, December 2009] 5

  6. Day 1 Ex 1: Black and White Balls (1/2) 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

  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. 1 swap 1 swap 1 swap Answer: 3 swaps [Programming Practicum, December 2009] 7

  8. Day 1 Ex 2: Dropping Ball Modelling of problem Using 2D array Using codes Using 1D array With sorting 0 1 2 3 4 0 1 2 3 4 Figure 1a. Figure 1b. [Programming Practicum, December 2009] 8

  9. Day 1 Ex 3: Class Schedule Modelling of problem 200 240 210 230 30 60 80 100 10 40 200 260 260 280 150 180 160 170 Longest lesson? Number of free periods? Maximum number of concurrent lessons? [Programming Practicum, December 2009] 9

  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: list • IfS = 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. • IfS = 6, there is no solution. 2 3 5 7 8 10 [Programming Practicum, December 2009]

  11. Task: Sum of 2 Elements (2/2) • Consider this algorithm: public static void find2Elements (int[] list, int sum) { for (inti=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. [Programming Practicum, December 2009]

  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(nlgn) [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) 16 11 9 10 5 6 8 1 2 4 [Programming Practicum, December 2009]

  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, 16 11 9 10 5 6 8 arr 1 2 4 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]. [Programming Practicum, December 2009]

  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 start 12 12 5 10 12 5 8 8 8 5 10 10 7 7 7 6 6 6 [Programming Practicum, December 2009]

  16. Heap sort (5/11) • Phase 1: Build heap (heapify) After Before 5 16 1 11 8 9 10 10 5 4 6 6 8 9 16 1 2 2 4 11 heapify(int[] arr) { for (inti=(arr.length– 2)/2; i>= 0; i--) siftDown(arr, i, arr.length-1); } [Programming Practicum, December 2009]

  17. Heap sort (6/11) heapify(int[] arr) { for (inti=(arr.length– 2)/2; i>= 0; i--) siftDown(arr, i, arr.length-1); } 5 5 5 5 1 1 1 1 8 8 8 9 16 16 10 10 11 11 4 11 6 6 6 6 9 9 9 8 siftDown(arr, 4, 9) siftDown(arr, 3, 9) siftDown(arr, 2, 9) Before 10 16 10 16 2 2 2 2 4 4 4 11 5 1 8 10 4 6 9 16 2 11 [Programming Practicum, December 2009]

  18. Heap sort (7/11) heapify(int[] arr) { for (inti=(arr.length – 2)/2; i>= 0; i--) siftDown(arr, i, arr.length-1); } …continue from previous slide 5 16 5 11 1 16 9 9 9 16 10 10 11 5 11 6 6 6 8 8 8 siftDown(arr, 1, 9) siftDown(arr, 0, 9) After 1 10 1 2 2 2 4 4 4 16 11 9 10 5 6 8 1 2 4 [Programming Practicum, December 2009]

  19. Heap sort (8/11) • Phase 2: Select largest value, swap, and adjust heap 11 11 10 4 2 16 for (int last = arr.length– 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1); } 10 10 5 10 11 11 9 9 9 9 9 9 4 4 4 4 10 10 5 5 5 5 5 2 6 6 6 6 6 6 8 8 8 8 8 8 1 1 1 1 1 1 11 2 2 2 2 11 16 4 16 16 16 16 swap swap siftDown siftDown last last [Programming Practicum, December 2009]

  20. Heap sort (9/11) for (int last = arr.length– 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1); } 1 9 10 1 6 9 8 8 1 5 5 5 5 5 5 5 5 5 8 6 6 6 8 1 8 9 9 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 6 8 1 6 8 6 6 6 1 9 9 1 9 8 1 8 9 9 last 10 10 10 10 1 10 10 10 10 11 11 11 11 11 11 11 11 11 16 16 16 16 16 16 16 16 16 swap swap swap siftDown siftDown siftDown last last [Programming Practicum, December 2009]

  21. Heap sort (10/11) for (int last = arr.length – 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1); } 5 2 6 4 5 2 4 2 1 2 5 4 1 2 5 4 2 4 4 1 4 1 1 1 1 1 1 last 4 2 5 5 4 5 2 5 5 6 2 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 16 16 16 16 16 16 16 16 16 swap swap swap siftDown siftDown siftDown last last [Programming Practicum, December 2009]

  22. Heap sort (11/11) for (int last = arr.length – 1; last>0; last--) { swap arr[0] with arr[last] siftDown(arr, 0, last-1); } 2 1 1 last 1 2 2 4 4 4 5 5 5 6 6 6 8 8 8 9 9 9 • Time complexity of Heap sort: • Phase 1 (heapify): O(n) • Phase 2: O(nlgn) • (Details of analysis left for CS1102) • Hence, overall: O(nlgn) • Implement Heap sort yourself • Animation website: http://www2.hawaii.edu/~copley/665/HSApplet.html 10 10 10 11 11 11 16 16 16 swap siftDown [Programming Practicum, December 2009]

  23. THE END

More Related