Midterm Review CSE 2011 Winter 2011 113 October 2015.
-
Upload
lionel-edwards -
Category
Documents
-
view
222 -
download
0
Transcript of Midterm Review CSE 2011 Winter 2011 113 October 2015.
![Page 1: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/1.jpg)
Midterm Review
CSE 2011
Winter 2011
1April 19, 2023
![Page 2: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/2.jpg)
Algorithm Analysis
Given an algorithm, compute its running time in terms of O, , and (if any).Usually the big-Oh running time is enough.
Given f(n) = 5n + 10, show that f(n) is O(n).Find c and n0
Compare the grow rates of 2 functions.Order the grow rates of several functions.
Use slide 14.Use L’Hôpital’s rule.
2
![Page 3: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/3.jpg)
Running Times of Loops
Nested for loops:If the exact number of iterations of each loop is known, multiply the numbers of iterations of the loops.
If the exact number of iterations of some loop is not known, “open” the loops and count the total number of iterations.
3
![Page 4: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/4.jpg)
Running Time of Recursive Methods
Could be just a hidden “for" or “while” loop.See “Tail Recursion” slide.“Unravel” the hidden loop to count the number
of iterations.
LogarithmicExamples: binary search, exponentiation, GCD
Solving a recurrenceExample: merge sort, quick sort
4
![Page 5: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/5.jpg)
Recursion
Know how to write recursive functions/methods:Recursive call
Adjusting recursive parameter(s) for each call
Base case(s)
1.Use the definition (factorial, tree depth, height)
2.Cut the problem size by half (binary search), or by k elements at a time (sum, reversing arrays).
3.Divide and conquer (merge sort, quick sort)5
![Page 6: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/6.jpg)
Sorting Algorithms
Insertion sortMerge sortQuick sortLower bound of sorting algorithms
O(NlogN)
When to use which sorting algorithm?Linear-time sorting (bucket sort)
6
![Page 7: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/7.jpg)
Running Time of Tree Methods
Most tree methods are recursive.Visualize the recursion trace.Count the number of nodes that were visited (processed) k.Compute the running time for processing each node O(m).If O(mv) ≠ O(mu), sum up the running times of the k nodes.Otherwise total running time = k O(m)
7
![Page 8: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/8.jpg)
Arrays and Linked Lists
ArraysA = BCloning arraysExtendable arraysStrategies for extending arrays:
doubling the sizeincrement by k cells
Linked listsSingly linkedDoubly linkedImplementationRunning times for insertion and deletion at the two ends.
8
![Page 9: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/9.jpg)
Running Times of Array and Linked List Operations
OperationArray
unsorted
Array
sorted
DL list
unsorted
DL list
sorted
insert O( ) O( ) O( ) O( )
delete O( ) O( ) O( ) O( )
search O( ) O( ) O( ) O( )
9
![Page 10: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/10.jpg)
Stacks, Queues and Deques
OperationsArray implementationLinked list implementationRunning times for each implementationAssignment 3 (deques)
10
![Page 11: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/11.jpg)
Trees
Definitions, terminologiesTraversal algorithms and applications Preorder Postorder
Computing depth and height of a tree or node.
11
![Page 12: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/12.jpg)
Binary Trees
Linked structure implementationArray implementationTraversal algorithms
Preorder PostorderInorder
Properties: relationships between n, i, e, h.
12
![Page 13: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/13.jpg)
Binary Search Trees
PropertiesSearchingInsertion
Distinct keysDuplicate keys
DeletionRunning times
13
![Page 14: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/14.jpg)
During Reading Week
Office hours:Friday, 25 February, 11:30-13:00CSEB-2024
Homework (trees)R-7.5, 7.6, 7.7, 7.16, 7.23C-7.4, 7.5, 7.6, 7.9 (C-7.23, 7.33, 7.34: optional, more difficult)R-10.1, 10.3, 10.4 Implement BST insertion method using the posted binary
tree Java template.
14
![Page 15: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/15.jpg)
Midterm Test
Date: Tuesday, March 1Time: 13:00-14:20 (80 minutes)Location: CB-121 (lecture room)
MaterialsLectures notes from the beginning up to and
including the lecture on February 17. Corresponding sections in the textbook. Assignments 1, 2 and 3.
15
![Page 16: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/16.jpg)
Test Rules
This is a closed-book test. No books, notes or calculators are allowed. You will be given blank paper for scrap work.
Bring a photo ID, pens, and pencils. You may use pencils with darkness of at least HB; 2B is preferred.
No questions are allowed during the test. You may leave the classroom if you hand in your test
booklet 15 minutes or more before the test ends. Programming problems will be marked based on both
correctness and efficiency. You may use either Java or Java-like pseudo-code.
16
![Page 17: Midterm Review CSE 2011 Winter 2011 113 October 2015.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649eb45503460f94bbcb62/html5/thumbnails/17.jpg)
Test Rules (2)
Be present at 12:55 P.M. Put away all notes and books (under the seats).Seating:
4 students per table (or 8 per row), except the first row.
6 students in the first row (or 3 per table).Remind your neighbour(s) to put away all notes
and books and follow the seating rule.