cs1020 data structures and algorithms i lecture note #14 wrapping up

24
CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

Upload: dylan-mitchell

Post on 18-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

CS1020 Data Structures and Algorithms ILecture Note #14

Wrapping Up

Page 2: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

Outline

2[CS1020 Lecture 14: Wrapping Up]

1. Past Years’ Exam Questions

2. Exam Matters

3. Announcements

Page 3: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 Past Years’ Exam Questions

Page 4: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2 We will discuss questions 1 to 8 here

For the rest, please discuss on the IVLE forum Note that exam answers are not released

4[CS1020 Lecture 14: Wrapping Up]

ArrayList<Integer> arr = new ArrayList<Integer>();

for (int i=0; i<10; i++)arr.add(i);

for (int i=0; i<arr.size()/2; i++) arr.add(i, arr.remove(arr.size()-1-i));

System.out.println(arr);

(A) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9](B) [9, 8, 7, 6, 5, 4, 3, 2, 1, 0](C) [9, 7, 5, 3, 1, 0, 2, 4, 6, 8](D) [9, 7, 5, 3, 1, 8, 6, 4, 2, 0](E) [9, 0, 8, 1, 7, 2, 6, 3, 5, 4]

Q1

Page 5: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2 Q2. Calling recurse(12);

5[CS1020 Lecture 14: Wrapping Up]

public static void recurse(int n) {System.out.println("The number is " + n);if (n-- > 0) {

recurse(n--);}

}

(A) 5(B) 6(C) 7(D) 12(E) None of the above.

Page 6: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2 Q3. How many integers in s can also be found in q?

6[CS1020 Lecture 14: Wrapping Up]

int[] array = new int[] {1,2,3,4,5};

Stack<Integer> s = new Stack<Integer>();for (int i: array) {

s.push(i);if (i%2 == 0) s.pop();

}

Queue<Integer> q = new LinkedList<Integer>();for (int i: array) {

q.offer(i);if (i%2 == 0) q.poll();

}

(A) 0(B) 1(C) 2(D) 3(E) 4

Page 7: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2 Q4. Result of first partitioning:

7[CS1020 Lecture 14: Wrapping Up]

2, 5, 3, 7, 9, 15, 20, 18, 25, 30, 10

(A) 3(B) 4(C) 5(D) 6(E) None of the above.

How many possible integers among the elements could be the pivot?

Page 8: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2 Q5. Shift elements in an n×n matrix by a shift amount.

8[CS1020 Lecture 14: Wrapping Up]

(A) (r*numCols + c + shiftAmt);(B) (r*numCols + c + shiftAmt) % numRows;(C) (r*numCols + c + shiftAmt) % numCols;(D) (r*numCols + c + shiftAmt) % (numRows*numCols);(E) None of the above.

Shift 3

1211109

8765

4321

arr

9876

5432

1121110

newArr

int[][] newArr = new int[arr.length][arr[0].length];int index, numRows = arr.length, numCols = arr[0].length;for (int r = 0; r < numRows; r++) {for (int c = 0; c < numCols; c++) {

index =

newArr[index/numCols][index%numCols] = arr[r][c];}

}

Page 9: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2 Q6. Which of the following statements is true?

9[CS1020 Lecture 14: Wrapping Up]

(A) Quadratic probing may avoid the clustering problem completely.

(B) A perfect hash function must distribute the keys evenly in the hash table.

(C) It is effective (i.e. there are fewer collisions) to use direct addressing table when the keys are not dense.

(D) The probe sequence of modified linear probing covers all positions in a hash table.

(E) None of the above.

Page 10: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2 Q7. searchAndSwitch(E item)

If item found not in the last node, return true and switch node containing item with its next node;

If item not found or item found is last node, return false.

10[CS1020 Lecture 14: Wrapping Up]

Example: [Abe, Gigi, Flora, Andy, Jack, Zoe]

After calling searchAndSwich(“Andy”):[Abe, Gigi, Flora, Jack, Andy, Zoe]

General case:… …

currprev next

Special case: node found is first node of list

curr next

head

Page 11: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2

11[CS1020 Lecture 14: Wrapping Up]

public boolean searchAndSwitch(E item) {ListNode <E> curr = head;ListNode <E> prev = null;while (curr != null &&

!curr.getElement().equals(item)) {prev = curr;curr = curr.getNext();

}

return true; }

Special case: node found is first node of list

curr next

head

General case:

… …

currprev next

Page 12: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

Let’s label the vertices of the given polygon v0 to vn-1, where n is the number of vertices, as shown in the diagrams above.

1 AY2013/4 Semester 2 Q8. triangulate()

Triangulation: to split a convex polygon into a number of triangles Triangulate a MyPolygon object into an ArrayList of triangles

12[CS1020 Lecture 14: Wrapping Up]

v0

v1

v2v3

v4

v0

v1

v2 v3

v4

v5

v6

v7

Page 13: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2

13[CS1020 Lecture 14: Wrapping Up]

import java.awt.Point;import java.util.ArrayList; class MyPolygon {

private int size; // number of verticesprivate ArrayList<Point> vertices;

 public MyPolygon(int num, ArrayList<Point> pts) {

size = num;vertices = new ArrayList<Point>(pts);

// Other methods not shown as they are not required// for this question

}

Page 14: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

v0

v1

v2v3

v4v0

v1

v2 v3

v4

v5

v6

v7

1 AY2013/4 Semester 2

14[CS1020 Lecture 14: Wrapping Up]

public ArrayList<MyPolygon> Triangulate() {ArrayList<MyPolygon> triangles;

return triangles; }

Page 15: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

1 AY2013/4 Semester 2

15[CS1020 Lecture 14: Wrapping Up]

public ArrayList<MyPolygon> Triangulate() {ArrayList<MyPolygon> triangles;

return triangles; }

v0

v1

v2v3

v4v0

v1

v2 v3

v4

v5

v6

v7

Alternative solution

Page 16: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

2 Exam Matters

Page 17: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

2 CS1020 Objectives (1/2) Give an introduction to OO Programming (OOP)

Model using Java programming language, linear data structures, and algorithms for constructing efficient programs.

Emphasize on data abstraction issues (through ADTs) in the code development.

Emphasize on efficient implementations of chosen data structures and algorithms.

17[CS1020 Lecture 14: Wrapping Up]

Page 18: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

2 CS1020 Objectives (2/2) Linear data structures include arrays, lists,

stacks, queues, and hash tables; together with their algorithms (insert, delete, find, and updates).

Simple algorithmic paradigms, such as sorting and search algorithms, and divide-and-conquer algorithms were introduced.

Recursion and elementary analysis of algorithms were taught.

18[CS1020 Lecture 14: Wrapping Up]

Page 19: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

2 Final Exam (1/2)

Date: 4 May 2015, Monday Time: 5 – 7pm Venue: To be announced by Registrar’s Office Weightage: 40% (see http://www.comp.nus.edu.sg/~

cs1020/1_module_info/desc.html for details)

Scope: Everything covered in lectures, tutorials and labs 30%: Materials up to and including Exceptions

(tested in mid-term) 70%: Materials after Exceptions

19[CS1020 Lecture 14: Wrapping Up]

Page 20: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

2 Final Exam (2/2) Format:

Section A (18 marks): 6 MCQs Section B (62 marks): 7 questions

Closed book. You are allowed to bring in one A4 handwritten reference sheet.

Calculators NOT allowed. You may use pencil to write your answers. Read through all questions first before

answering them. Refer to CS1020 website, “Exams” page:

http://www.comp.nus.edu.sg/~cs1020/3_ca/exams.html

20[CS1020 Lecture 14: Wrapping Up]

Page 21: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

3 Announcements

Page 22: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

3 Announcements Our consultations hours are given on the IVLE

announcement Please check out the IVLE as we will still be

making announcements We will post your CA marks for your checking Please check your CA marks and report any

discrepancy before the stated deadline

22[CS1020 Lecture 14: Wrapping Up]

Page 23: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

23[CS1020 Lecture 14: Wrapping Up]

Page 24: CS1020 Data Structures and Algorithms I Lecture Note #14 Wrapping Up

End of file