Professional Documents
Culture Documents
Array-Based Implementation of List
Array-Based Implementation of List
Chandrabose Aravindan
<AravindanC@ssn.edu.in>
snu-logo
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
snu-logo
C. Aravindan (SSN)
Array and List
46 46
0
S
21 21
47 47
21 21
48 48
1
A
21 21
49 49
21 21
50 50
2
M 21
51
21
51
21 21
3 52 52
P
21 21
Data Structures
53 53
21 21
54 54
4
L
21 21
55 55
21 21
56 56
5
E
21 21
57 57
21 21
58 58
21 21
59 59
April 02, 2024
21 21
60 60
snu-logo
2 / 27
Array and List
snu-logo
from a r r a y i m p o r t a r r a y
class ArrayList :
snu-logo
snu-logo
'Janet' 'Jonas'
'Joseph' 'Helen'
'Rene' 'Virginia'
0 1 2 3 4 5
snu-logo
snu-logo
import ctypes
class ArrayList :
snu-logo
snu-logo
class ArrayList :
d e f __len__ ( s e l f ) :
return s e l f . _size
d e f __getitem__ ( s e l f , i n d e x ) :
i f ( n o t 0 <= i n d e x < s e l f . _ s i z e ) :
r a i s e I n d e x E r r o r ( " Index ␣ i s ␣ out ␣ of ␣ range ! " )
r e t u r n s e l f . _items [ i n d e x ]
snu-logo
snu-logo
class ArrayList :
def isEmpty ( s e l f ) :
" " " T h i s method i s a c t u a l l y n o t r e q u i r e d .
Defined only f o r academic purpose .
D e f i n i t i o n o f __len__ e n s u r e s t h a t b o o l ( l s t )
is defined !
"""
r e t u r n ( s e l f . _ s i z e == 0 )
snu-logo
snu-logo
class ArrayList :
def begin ( s e l f ) :
return 0
d e f end ( s e l f ) :
return s e l f . _size
d e f next ( s e l f , pos ) :
i f ( n o t 0 <= i n d e x < s e l f . _ s i z e ) :
r a i s e I n d e x E r r o r ( " Index ␣ i s ␣ out ␣ of ␣ range ! " )
r e t u r n ( po s + 1 )
snu-logo
snu-logo
snu-logo
d e f append ( s e l f , i t e m ) :
s e l f . _items [ s e l f . _ s i z e ] = item
s e l f . _ s i z e += 1
snu-logo
snu-logo
class ArrayList :
d e f append ( s e l f , i t e m ) :
i f ( s e l f . _ s i z e == s e l f . _ c a p a c i t y ) :
raise IndexError ( ’ List ␣ is ␣ already ␣ f u l l ! ’ )
s e l f . _items [ s e l f . _ s i z e ] = item
s e l f . _ s i z e += 1
snu-logo
snu-logo
snu-logo
A A
B B A
snu-logo
snu-logo
class ArrayList :
d e f append ( s e l f , i t e m ) :
i f ( s e l f . _ s i z e == s e l f . _ c a p a c i t y ) :
s e l f . _ r e s i z e (2 ∗ s e l f . _capacity )
s e l f . _items [ s e l f . _ s i z e ] = item
s e l f . _ s i z e += 1
snu-logo
snu-logo
snu-logo
class ArrayList :
snu-logo
Most of the time, the ‘append()’ method runs in constant time O(1)
snu-logo
Most of the time, the ‘append()’ method runs in constant time O(1)
Occasionally, resizing is required and the complexity is O(n)
snu-logo
Most of the time, the ‘append()’ method runs in constant time O(1)
Occasionally, resizing is required and the complexity is O(n)
In such cases, amortized analysis provides a better upper bound
snu-logo
Most of the time, the ‘append()’ method runs in constant time O(1)
Occasionally, resizing is required and the complexity is O(n)
In such cases, amortized analysis provides a better upper bound
Analyze the time complexity for m successive operations and amortize
the time equally for each individual operation.
snu-logo
snu-logo
snu-logo
snu-logo
0 1 2 3 4 5 6 7
$
$
snu-logo
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C. Aravindan (SSN) Data Structures April 02, 2024 17 / 27
Amortized Analysis
Consider a dynamic array based implementation of List ADT, with
initial array capacity of 1, which is doubled when required
snu-logo
snu-logo
snu-logo
snu-logo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
current number of elements
C. Aravindan (SSN) Data Structures April 02, 2024 18 / 27
Geometric Vs Arithmetic Progressions
The fact that the capacity of the array is geometrically increasing is
important!
snu-logo
snu-logo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
current number of elements snu-logo
snu-logo
snu-logo
snu-logo
snu-logo
0 1 2 k n−1
snu-logo
snu-logo
class ArrayList :
snu-logo
snu-logo
0 1 2 k n−1
snu-logo
snu-logo
class ArrayList :
snu-logo
snu-logo
snu-logo
snu-logo
snu-logo
snu-logo
snu-logo
snu-logo
snu-logo
snu-logo