winter 2016 comp-250: introduction to computer science
TRANSCRIPT
![Page 1: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/1.jpg)
Winter 2016COMP-250: Introduction
to Computer ScienceLecture 20, March 24, 2016
![Page 2: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/2.jpg)
————— Public Announcement —————
————— ———————— —————
![Page 3: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/3.jpg)
————— Public Announcement —————
————— ———————— —————
ES EXAM OFFICE: [email protected] TEL. 514-398-4209
FINAL EXAMINATION CHECKLIST - April 2016 A signed original plus one copy of the exam is to be delivered to the Exams Office room MS-72 (3415 McTavish). In order to have your exam printed by the Exam Office, it must be submitted by: March 24th, 2016 Course : COMP 250 Sect: 1
Title: Intro to Computer Science
Exam Date: 4/28/2016
Exam Time: 2:00:00 PM
Examiner: Name: Prof. C. Crépeau
Office #:
Email:
Examiner’s signature:
Cell #:
Associate Examiner:
Name: Tel #: Email:
Associate Examiner’s signature:
Cell #:
INFORMATION AND AUTHORIZATION OF MATERIALS: 1. Is this a CLOSED BOOK or OPEN BOOK exam? CLOSED OPEN
2. Is this a MULTIPLE-CHOICE Exam? Multiple versions must be submitted, regardless of the number of students, questions or whether the exam is only partially multiple-choice (4 versions for enrolments of 200+). YES NO
3. Is your MULTIPLE-CHOICE exam: ANSWER ON EXAM MACHINE SCORED ON ANSWER SHEETS
4. If MACHINE-SCORED, number of versions: _______
5. DICTIONARIES: Translation ONLY Regular ONLY ANY NONE
6. CALCULATORS: Non-programmable Faculty Standard ANY NONE
7. RETURN EXAM? If “YES”, include space for Student NAME and Student NUMBER on cover page YES NO
8. Answer in EXAM BOOKLET YES NO
9. Answer directly ON EXAM YES NO 10. Specify what materials are permitted (crib sheets/textbook/notes etc) or any SPECIAL REQUIREMENTS/INSTRUCTIONS for the exam. If crib sheets are permitted, please specify if they are typed or handwritten and if they can be double-sided or only single-sided.
(For office use only)
Date received:
______________________________
# OF PAGES:
V1 __________ V2__________ V3__________ V4__________
ES EXAM OFFICE: [email protected] TEL. 514-398-4209
FINAL EXAMINATION CHECKLIST - April 2016 A signed original plus one copy of the exam is to be delivered to the Exams Office room MS-72 (3415 McTavish). In order to have your exam printed by the Exam Office, it must be submitted by: March 24th, 2016 Course : COMP 250 Sect: 1
Title: Intro to Computer Science
Exam Date: 4/28/2016
Exam Time: 2:00:00 PM
Examiner: Name: Prof. C. Crépeau
Office #:
Email:
Examiner’s signature:
Cell #:
Associate Examiner:
Name: Tel #: Email:
Associate Examiner’s signature:
Cell #:
INFORMATION AND AUTHORIZATION OF MATERIALS: 1. Is this a CLOSED BOOK or OPEN BOOK exam? CLOSED OPEN
2. Is this a MULTIPLE-CHOICE Exam? Multiple versions must be submitted, regardless of the number of students, questions or whether the exam is only partially multiple-choice (4 versions for enrolments of 200+). YES NO
3. Is your MULTIPLE-CHOICE exam: ANSWER ON EXAM MACHINE SCORED ON ANSWER SHEETS
4. If MACHINE-SCORED, number of versions: _______
5. DICTIONARIES: Translation ONLY Regular ONLY ANY NONE
6. CALCULATORS: Non-programmable Faculty Standard ANY NONE
7. RETURN EXAM? If “YES”, include space for Student NAME and Student NUMBER on cover page YES NO
8. Answer in EXAM BOOKLET YES NO
9. Answer directly ON EXAM YES NO 10. Specify what materials are permitted (crib sheets/textbook/notes etc) or any SPECIAL REQUIREMENTS/INSTRUCTIONS for the exam. If crib sheets are permitted, please specify if they are typed or handwritten and if they can be double-sided or only single-sided.
(For office use only)
Date received:
______________________________
# OF PAGES:
V1 __________ V2__________ V3__________ V4__________
![Page 4: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/4.jpg)
——
——
— P
ublic
Ann
ounc
emen
t —
——
——
——
——
— —
——
——
——
— —
——
——
![Page 5: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/5.jpg)
6.1Heaps I
HEAPS I• Heaps
• Properties
• Insertion and Deletion
![Page 6: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/6.jpg)
6.2Heaps I
Heaps• A heap is a binary tree T that stores a collection of
keys (or key-element pairs) at its internal nodes andthat satisfies two additional properties:- Order Property: key(parent) ≤ key(child)- Structural Property: all levels are full, except the
last one, which is left-filled (complete binary tree)
4
6
207
811
5
9
1214
15
2516
![Page 7: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/7.jpg)
6.3Heaps I
Not Heaps• bottom level is not left-filled
• key(parent)> key(child)
4
6
207
811
5
9
1214
15
2516
4
6
207
811
9
5
1214
15
2516
![Page 8: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/8.jpg)
6.3Heaps I
Not Heaps• bottom level is not left-filled
• key(parent)> key(child)
4
6
207
811
5
9
1214
15
2516
4
6
207
811
9
5
1214
15
2516
6.3Heaps I
Not Heaps• bottom level is not left-filled
• key(parent)> key(child)
4
6
207
811
5
9
1214
15
2516
4
6
207
811
9
5
1214
15
2516
![Page 9: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/9.jpg)
6.4Heaps I
Height of a HeapA heap T storing n keys has height h = log(n + 1),which is O(log n)
• n ≥ 1 + 2 + 4 + ... + 2h-2 + 1 = 2h-1 - 1 + 1 = 2h-1
• n ≤ 1 + 2 + 4 + ... +2h-1 = 2h - 1
• Therefore 2h-1 ≤ n ≤ 2h - 1
• Taking logs, we get log (n + 1) ≤ h ≤ log n + 1
• Which implies h = log(n+1)
4
6
207
5
915
16h − 1
01
h − 2
h
h − 1
01
h − 2
h
4
6
207
811
5
9
1214
15
2516 2231
![Page 10: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/10.jpg)
6.4Heaps I
Height of a HeapA heap T storing n keys has height h = log(n + 1),which is O(log n)
• n ≥ 1 + 2 + 4 + ... + 2h-2 + 1 = 2h-1 - 1 + 1 = 2h-1
• n ≤ 1 + 2 + 4 + ... +2h-1 = 2h - 1
• Therefore 2h-1 ≤ n ≤ 2h - 1
• Taking logs, we get log (n + 1) ≤ h ≤ log n + 1
• Which implies h = log(n+1)
4
6
207
5
915
16h − 1
01
h − 2
h
h − 1
01
h − 2
h
4
6
207
811
5
9
1214
15
2516 2231
6.4Heaps I
Height of a HeapA heap T storing n keys has height h = log(n + 1),which is O(log n)
• n ≥ 1 + 2 + 4 + ... + 2h-2 + 1 = 2h-1 - 1 + 1 = 2h-1
• n ≤ 1 + 2 + 4 + ... +2h-1 = 2h - 1
• Therefore 2h-1 ≤ n ≤ 2h - 1
• Taking logs, we get log (n + 1) ≤ h ≤ log n + 1
• Which implies h = log(n+1)
4
6
207
5
915
16h − 1
01
h − 2
h
h − 1
01
h − 2
h
4
6
207
811
5
9
1214
15
2516 2231
![Page 11: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/11.jpg)
6.5Heaps I
3
74
21 10 20 8
22 28 13 25
Heap Insertion
So here we go ...
The key to insert is 6
19
![Page 12: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/12.jpg)
6.6Heaps I
3
74
21 10 20 8
22 28 13 19
Add the key in the next available position in theheap.
Now begin Upheap.
Heap Insertion
25 6
![Page 13: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/13.jpg)
6.7Heaps I
Upheap• Swap parent-child keys out of order
374
21 10 20 8
22 28 13 19 25 6
374
21 10 6 8
22 28 13 19 25 20
![Page 14: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/14.jpg)
6.7Heaps I
Upheap• Swap parent-child keys out of order
374
21 10 20 8
22 28 13 19 25 6
374
21 10 6 8
22 28 13 19 25 20
6.7Heaps I
Upheap• Swap parent-child keys out of order
374
21 10 20 8
22 28 13 19 25 6
374
21 10 6 8
22 28 13 19 25 20
![Page 15: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/15.jpg)
6.8Heaps I
Upheap Continues
374
21 10
20
8
22 28 13 19 25
6
364
21 10
20
8
22 28 13 19 25
7
![Page 16: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/16.jpg)
6.8Heaps I
Upheap Continues
374
21 10
20
8
22 28 13 19 25
6
364
21 10
20
8
22 28 13 19 25
7
6.8Heaps I
Upheap Continues
374
21 10
20
8
22 28 13 19 25
6
364
21 10
20
8
22 28 13 19 25
7
![Page 17: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/17.jpg)
6.9Heaps I
• Upheap terminates when new key is greaterthan the key of its parent or the top of the heapis reached
• (total #swaps) ≤ (h − 1), which is O(log n)
3
7
4
21 10
20
8
22 28 13 19 25
6
End of Upheap
![Page 18: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/18.jpg)
6.10Heaps I
Removal From a HeapRemoveMin()
7
4
21 10 8
22 28 13 19 25
6
• The removal of the top key leaves a hole• We need to fix the heap• First, replace the hole with the last key in
the heap• Then, begin Downheap
3
20
![Page 19: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/19.jpg)
6.10Heaps I
Removal From a HeapRemoveMin()
7
4
21 10 8
22 28 13 19 25
6
• The removal of the top key leaves a hole• We need to fix the heap• First, replace the hole with the last key in
the heap• Then, begin Downheap
3
20
![Page 20: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/20.jpg)
6.11Heaps I
Downheap20
7
4
21 10 8
22 28 13 19 25
6
Downheap compares the parent with the smallestchild. If the child is smaller, it switches the two.
4
7
20
21 10 8
22 28 13 19 25
6
![Page 21: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/21.jpg)
6.11Heaps I
Downheap20
7
4
21 10 8
22 28 13 19 25
6
Downheap compares the parent with the smallestchild. If the child is smaller, it switches the two.
4
7
20
21 10 8
22 28 13 19 25
66.11Heaps I
Downheap20
7
4
21 10 8
22 28 13 19 25
6
Downheap compares the parent with the smallestchild. If the child is smaller, it switches the two.
4
7
20
21 10 8
22 28 13 19 25
6
![Page 22: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/22.jpg)
6.12Heaps I
Downheap Continues
4
7
20
21 10 8
22 28 13 19 25
6
4
7
10
21 20 8
22 28 13 19 25
6
![Page 23: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/23.jpg)
6.12Heaps I
Downheap Continues
4
7
20
21 10 8
22 28 13 19 25
6
4
7
10
21 20 8
22 28 13 19 25
6
6.12Heaps I
Downheap Continues
4
7
20
21 10 8
22 28 13 19 25
6
4
7
10
21 20 8
22 28 13 19 25
6
![Page 24: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/24.jpg)
6.13Heaps I
Downheap Continues
4
7
10
21 20 8
22 28 13 19 25
6
4
7
10
21 13 8
22 28 20 19 25
6
![Page 25: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/25.jpg)
6.13Heaps I
Downheap Continues
4
7
10
21 20 8
22 28 13 19 25
6
4
7
10
21 13 8
22 28 20 19 25
6
6.13Heaps I
Downheap Continues
4
7
10
21 20 8
22 28 13 19 25
6
4
7
10
21 13 8
22 28 20 19 25
6
![Page 26: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/26.jpg)
6.14Heaps I
End of Downheap4
7
10
21 13 8
22 28 20 19 25
6
• Downheap terminates when the key is greaterthan the keys of both its children or the bottomof the heap is reached.
• (total #swaps) ≤ (h − 1), which is O(log n)
![Page 27: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/27.jpg)
6.1Heaps II
HEAPS II• Implementation
• HeapSort
• Bottom-Up Heap Construction
• Locators
![Page 28: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/28.jpg)
6.2Heaps II
Implementation of a Heappublic class HeapPriorityQueue implements PriorityQueue{
BinaryTree T;Position last;Comparator comparator;...
}
lastheap
(4,C)
(6,Z)
(20,B)(7,Q)
(8,W)(11,S)
(5,A)
(9,F)
(12,H)(14,E)
(15,K)
(25,J)(16,X)
comp
<=>
![Page 29: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/29.jpg)
6.3Heaps II
Implementation of a Heap(cont.)• Two ways to find the insertion position z in a heap:
(2,C)
(4,C)
(6,Z)(7,Q)
(8,W)(11,S)
(5,A)
(9,F)
(12,H)(14,E)
(15,K)
(25,J)(16,X) (20,B) (10,L)
z
(4,C)
(6,Z)
(20,B)(7,Q)
(5,A)
(9,F)
(12,H)(14,E)
(15,K)
(25,J)(16,X)w z
u
![Page 30: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/30.jpg)
6.3Heaps II
Implementation of a Heap(cont.)• Two ways to find the insertion position z in a heap:
(2,C)
(4,C)
(6,Z)(7,Q)
(8,W)(11,S)
(5,A)
(9,F)
(12,H)(14,E)
(15,K)
(25,J)(16,X) (20,B) (10,L)
z
(4,C)
(6,Z)
(20,B)(7,Q)
(5,A)
(9,F)
(12,H)(14,E)
(15,K)
(25,J)(16,X)w z
u6.3Heaps II
Implementation of a Heap(cont.)• Two ways to find the insertion position z in a heap:
(2,C)
(4,C)
(6,Z)(7,Q)
(8,W)(11,S)
(5,A)
(9,F)
(12,H)(14,E)
(15,K)
(25,J)(16,X) (20,B) (10,L)
z
(4,C)
(6,Z)
(20,B)(7,Q)
(5,A)
(9,F)
(12,H)(14,E)
(15,K)
(25,J)(16,X)w z
u
![Page 31: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/31.jpg)
6.4Heaps II
Vector Based Implementation• Updates in the underlying tree occur only at the “last
element”
• A heap can be represented by a vector, where thenode at rank i has- left child at rank 2i and- right child at rank 2i + 1
• The leaves do no need to be explicitly stored
• Insertion and removals into/from the heapcorrespond to insertLast and removeLast on thevector, respectively
1
2
5 6 7
3
4
8 9 10 11 12 13
![Page 32: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/32.jpg)
6.4Heaps II
Vector Based Implementation• Updates in the underlying tree occur only at the “last
element”
• A heap can be represented by a vector, where thenode at rank i has- left child at rank 2i and- right child at rank 2i + 1
• The leaves do no need to be explicitly stored
• Insertion and removals into/from the heapcorrespond to insertLast and removeLast on thevector, respectively
1
2
5 6 7
3
4
8 9 10 11 12 13
6.4Heaps II
Vector Based Implementation• Updates in the underlying tree occur only at the “last
element”
• A heap can be represented by a vector, where thenode at rank i has- left child at rank 2i and- right child at rank 2i + 1
• The leaves do no need to be explicitly stored
• Insertion and removals into/from the heapcorrespond to insertLast and removeLast on thevector, respectively
1
2
5 6 7
3
4
8 9 10 11 12 13
![Page 33: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/33.jpg)
6.5Heaps II
Heap Sort• All heap methods run in logarithmic time or better
• If we implement PriorityQueueSort using a heap forour priority queue, insertItem and removeMin eachtake O(log k), k being the number of elements in theheap at a given time.
• We always have at most n elements in the heap, sothe worst case time complexity of these methods isO(log n).
• Thus each phase takes O(n log n) time, so thealgorithm runs in O(n log n) time also.
• This sort is known as heap-sort.
• The O(n log n) run time of heap-sort is much betterthan the O(n2) run time of selection and insertionsort.
In-Place Heap-Sort• Do not use an external heap
• Embed the heap into the sequence, using the vectorrepresentation
![Page 34: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/34.jpg)
6.6Heaps II
Bottom-Up Heap Construction• build (n + 1)/2 trivial one-element heaps
• now build three-element heaps on top of them
61216 23 204 715
27
612
25
16 23 204
5
7
11
15
![Page 35: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/35.jpg)
6.6Heaps II
Bottom-Up Heap Construction• build (n + 1)/2 trivial one-element heaps
• now build three-element heaps on top of them
61216 23 204 715
27
612
25
16 23 204
5
7
11
15
6.6Heaps II
Bottom-Up Heap Construction• build (n + 1)/2 trivial one-element heaps
• now build three-element heaps on top of them
61216 23 204 715
27
612
25
16 23 204
5
7
11
15
![Page 36: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/36.jpg)
6.7Heaps II
Bottom-Up Heap Construction• downheap to preserve the order property
• now form seven-element heaps
20
1112
15
2516 23 275
4
7
6
20
8
1112
15
2516 23 275
9
4
7
6
![Page 37: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/37.jpg)
6.7Heaps II
Bottom-Up Heap Construction• downheap to preserve the order property
• now form seven-element heaps
20
1112
15
2516 23 275
4
7
6
20
8
1112
15
2516 23 275
9
4
7
6
6.7Heaps II
Bottom-Up Heap Construction• downheap to preserve the order property
• now form seven-element heaps
20
1112
15
2516 23 275
4
7
6
20
8
1112
15
2516 23 275
9
4
7
6
![Page 38: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/38.jpg)
6.8Heaps II
Bottom-Up Heap Construction(cont.)
6
207
81112
15
2516 23 27
5
9
4
6
207
81112
14
15
2516 23 27
5
9
4
![Page 39: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/39.jpg)
6.8Heaps II
Bottom-Up Heap Construction(cont.)
6
207
81112
15
2516 23 27
5
9
4
6
207
81112
14
15
2516 23 27
5
9
46.8Heaps II
Bottom-Up Heap Construction(cont.)
6
207
81112
15
2516 23 27
5
9
4
6
207
81112
14
15
2516 23 27
5
9
4
![Page 40: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/40.jpg)
6.9Heaps II
Bottom-Up Heap Construction(cont.)
The End
4
6
207
811
5
9
1214
15
2516 23 27
![Page 41: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/41.jpg)
6.10Heaps II
Analysis of Bottom-Up HeapConstruction
• Proposition: Bottom-up heap construction with nkeys takes O(n) time.- Insert (n + 1)/2 nodes- Insert (n + 1)/4 nodes and downheap them- Insert (n + 1)/8 nodes and downheap them- ...- visual analysis:
• n inserts, n/2 upheaps with total O(n) running time
4
6
207
811
5
9
1214
15
2516 23 27
6.10Heaps II
Analysis of Bottom-Up HeapConstruction
• Proposition: Bottom-up heap construction with nkeys takes O(n) time.- Insert (n + 1)/2 nodes- Insert (n + 1)/4 nodes and downheap them- Insert (n + 1)/8 nodes and downheap them- ...- visual analysis:
• n inserts, n/2 upheaps with total O(n) running time
4
6
207
811
5
9
1214
15
2516 23 27
![Page 42: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/42.jpg)
Chapter 8
![Page 43: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/43.jpg)
⟨a1,a2,a3⟩
⟨a3,a1,a2⟩⟨a1,a3,a2⟩⟨a1,a2,a3⟩
⟨a1,a3,a2⟩ ⟨a3,a1,a2⟩
⟨a3,a1,a2⟩⟨a1,a3,a2⟩
⟨a3,a1,a2⟩⟨a2,a1,a3⟩⟨a1,a2,a3⟩
⟨a3,a2,a1⟩⟨a2,a3,a1⟩⟨a1,a3,a2⟩
⟨a2,a1,a3⟩
⟨a3,a2,a1⟩⟨a2,a3,a1⟩
⟨a2,a1,a3⟩
⟨a3,a2,a1⟩⟨a2,a3,a1⟩
⟨a2,a3,a1⟩ ⟨a3,a2,a1⟩
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
![Page 44: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/44.jpg)
N!
N!/2 N!/2
N!/4 N!/4 N!/4 N!/4
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
log N! ∈ 𝜽(N log N)
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩ ⟨a1,a2,a3⟩⟨a1,a2,a3⟩
⟨a1,a2,a3⟩ ⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
⟨a1,a2,a3⟩⟨a1,a2,a3⟩
⟨a1,a2,a3⟩
![Page 45: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/45.jpg)
Winter 2016COMP-250: Introduction
to Computer ScienceLecture 20, March 24, 2016
![Page 46: Winter 2016 COMP-250: Introduction to Computer Science](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fca286509ee039ae0166a5/html5/thumbnails/46.jpg)
Hardik Omar Faiz
Faizy Chris David B. David B.R.
Lekan
DoYeon