winter 2016 comp-250: introduction to computer...
TRANSCRIPT
![Page 1: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/1.jpg)
Winter 2016COMP-250: Introduction
to Computer ScienceLecture 6, January 28, 2016
![Page 2: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/2.jpg)
Java Generics
element next
|_,•|
![Page 3: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/3.jpg)
Java Generics
![Page 4: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/4.jpg)
Java Generics
![Page 5: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/5.jpg)
(Doubly) Linked List
![Page 6: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/6.jpg)
(Doubly) Linked List Node
element next prev
|9,•,•|
![Page 7: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/7.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
![Page 8: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/8.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
![Page 9: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/9.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
![Page 10: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/10.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
![Page 11: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/11.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
![Page 12: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/12.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 13: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/13.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 14: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/14.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 15: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/15.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 16: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/16.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 17: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/17.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
![Page 18: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/18.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
![Page 19: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/19.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
⊠
![Page 20: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/20.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
⊠
![Page 21: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/21.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
⊠ ⊠
![Page 22: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/22.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 23: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/23.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 24: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/24.jpg)
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
![Page 25: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/25.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
dummy dummy . Head Tail size
|•,•,2|
![Page 26: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/26.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
![Page 27: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/27.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
![Page 28: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/28.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
![Page 29: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/29.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
![Page 30: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/30.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
![Page 31: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/31.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
![Page 32: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/32.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
![Page 33: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/33.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
⊠
![Page 34: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/34.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
⊠
![Page 35: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/35.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
⊠ ⊠
![Page 36: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/36.jpg)
Array vs Linked List
![Page 37: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/37.jpg)
Linked Lists as ADT(Abstract Data Type)
![Page 38: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/38.jpg)
Linked List operations
![Page 39: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/39.jpg)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
dummy dummy . Head Tail size
|•,•,2|
Java LinkedList
• implemented as doubly linked list(with dummies)
• Node class is private
![Page 40: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/40.jpg)
Java LinkedList
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
|•,•,2|
![Page 41: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/41.jpg)
Java LinkedList
}expensive
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
|•,•,2|
![Page 42: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/42.jpg)
Java LinkedList
Time(n) is 𝜴(n2)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
|•,•,2|
![Page 43: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/43.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
![Page 44: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/44.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
![Page 45: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/45.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• when new space is needed - doubles the array size.
|•,•,•,•,•,•,•,•|
element
|•|
![Page 46: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/46.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• smaller array is copied into bigger array.
|•,•,•,•,⊠,⊠,⊠,⊠|
element
|•|
![Page 47: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/47.jpg)
array size cap.
|•,4,8|
Java ArrayList
• new element added to new array
|•,•,•,•,⊠,⊠,⊠,⊠|
element
|•|
![Page 48: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/48.jpg)
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
![Page 49: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/49.jpg)
Cost ?
![Page 50: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/50.jpg)
|•,•,•,•|
array size cap.
|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
![Page 51: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/51.jpg)
|•,•,•,•|
array size cap.
|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationExtra cost of 2k+1 new steps only if 2k stepsalready spent before!
![Page 52: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/52.jpg)
|•,•,•,•|
array size cap.
|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationExtra cost of 2k+1 new steps only if 2k stepsalready spent before!
Cost is amortized constant time!
![Page 53: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/53.jpg)
|•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
|•,•,•,•| |•,•| |•|20
21
22
23
![Page 54: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/54.jpg)
|•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
To build an arrayof 2k elements
you need 2k+1-1 steps!
|•,•,•,•| |•,•| |•|20
21
22
23
![Page 55: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/55.jpg)
|•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
To build an arrayof 2k elements
you need 2k+1-1 steps!
Cost is amortized constant time!
|•,•,•,•| |•,•| |•|20
21
22
23
![Page 56: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/56.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
![Page 57: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/57.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
![Page 58: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/58.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
Cost is constant!
![Page 59: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/59.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
![Page 60: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/60.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
![Page 61: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/61.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
![Page 62: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/62.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
![Page 63: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/63.jpg)
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
![Page 64: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/64.jpg)
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
![Page 65: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/65.jpg)
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
![Page 66: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/66.jpg)
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻
![Page 67: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/67.jpg)
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻⤻
![Page 68: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/68.jpg)
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻⤻⤻
![Page 69: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/69.jpg)
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻⤻⤻⤻
![Page 70: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/70.jpg)
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
![Page 71: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/71.jpg)
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
![Page 72: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/72.jpg)
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| 5
![Page 73: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/73.jpg)
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| Cost is linear!5
![Page 74: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/74.jpg)
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| Cost is linear!
Same for Remove!5
![Page 75: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/75.jpg)
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| Cost is linear!
Same for Remove!5
![Page 76: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/76.jpg)
LinkedList vs ArrayList
![Page 77: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January](https://reader035.vdocument.in/reader035/viewer/2022071001/5fbd48a6bd570e08125b3466/html5/thumbnails/77.jpg)
Winter 2016COMP-250: Introduction
to Computer ScienceLecture 6, January 28, 2016