0% found this document useful (0 votes)
23 views

4 5.1 Array-Based Sequences

The document discusses array-based sequences in Python, highlighting built-in types such as lists, tuples, and strings, which utilize arrays for storage. It explains operations like insertion and removal, detailing their time complexities, and compares incremental and doubling strategies for dynamic array resizing. The document also includes a brief overview of Python's implementation of dynamic arrays and their functionalities.

Uploaded by

nhonhm26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

4 5.1 Array-Based Sequences

The document discusses array-based sequences in Python, highlighting built-in types such as lists, tuples, and strings, which utilize arrays for storage. It explains operations like insertion and removal, detailing their time complexities, and compares incremental and doubling strategies for dynamic array resizing. The document also includes a brief overview of Python's implementation of dynamic arrays and their functionalities.

Uploaded by

nhonhm26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Array-Based Sequences

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 1


Python Sequence Classes
q Python has built-in types, list, tuple, and str.
q Each of these sequence types supports indexing to
access an individual element of a sequence, using a
syntax such as A[i]
q Each of these types uses an array to represent the
sequence.
n An array is a set of memory locations that can be
addressed using consecutive indices, which, in Python,
start with index 0.

A
0 1 2 i n
© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 2
Arrays of Characters or
Object References
q An array can store primitive elements, such as
characters, giving us a compact array.

q An array can also store references to objects.

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 3


Compact Arrays
q Primary support for compact arrays is in a
module named array.
n That module defines a class, also named array,
providing compact storage for arrays of primitive
data types.
q The constructor for the array class requires a
type code as a first parameter, which is a
character that designates the type of data that
will be stored in the array.

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 4


Type Codes in the array Class
q Python’s array class has the following
type codes:

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 5


Insertion
q In an operation insert(i, o), we need to make
room for the new element by shifting forward the
n − i elements A[i], …, A[n − 1]
q In the worst case (i = 0), this takes O(n) time

A
0 1 2 i n
A
0 1 2 i n
A o
0 1 2 i n
© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 6
© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 7
Element Removal
q In an operation remove(i), we need to fill the hole left by
the removed element by shifting backward the n − i − 1
elements A[i + 1], …, A[n − 1]
q In the worst case (i = 0), this takes O(n) time

A o
0 1 2 i n
A
0 1 2 i n
A
0 1 2 i n
© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 8
Performance
q In an array based implementation of a
dynamic list:
n The space used by the data structure is O(n)
n Indexing the element at I takes O(1) time
n add and remove run in O(n) time in worst case
q In an add operation, when the array is full,
instead of throwing an exception, we can
replace the array with a larger one…

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 9


Growable Array-based Array List
q In an add(o) operation
(without an index), we could Algorithm add(o)
always add at the end: if t = S.length − 1 then
append(o) A ← new array of
q When the array is full, we size …
replace the array with a for i ← 0 to n−1 do
larger one A[i] ← S[i]
S←A
q How large should the new
array be? n←n+1
S[n−1] ← o
n Incremental strategy: increase
the size by a constant c
n Doubling strategy: double the
size
© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 10
Comparison of the Strategies
q We compare the incremental strategy and
the doubling strategy by analyzing the total
time T(n) needed to perform a series of n
add(o) operations
q We assume that we start with an empty
stack represented by an array of size 1
q We call amortized time of an add operation
the average time taken by an add over the
series of operations, i.e., T(n)/n

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 11


Incremental Strategy Analysis
q We replace the array k = n/c times
q The total time T(n) of a series of n add
operations is proportional to
n + c + 2c + 3c + 4c + … + kc =
n + c(1 + 2 + 3 + … + k) =
n + ck(k + 1)/2
q Since c is a constant, T(n) is O(n + k2), i.e.,
O(n2)
q The amortized time of an add operation is O(n)

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 12


Doubling Strategy Analysis
q We replace the array k = log2 n
times
geometric series
q The total time T(n) of a series of n
add operations is proportional to 2
n + 1 + 2 + 4 + 8 + …+ 2k = 4
1 1
n+2 k + 1 −1 =
3n − 1
8
q T(n) is O(n)
q The amortized time of an add
operation is O(1)
© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 13
Python Implementation
q Viết chương trình mô phỏng mảng động. Mảng
động được biểu diễn thông qua: kích thước thực
tế của mảng, kích thước tối đa và các phần tử
được biểu diễn bằng mảng. Mảng động có các
chắc năng như tạo mảng, thay đổi kích thước,
thêm 1 phần tử vào cuối mảng, thêm 1 phần tử
vào 1 vị trí bất kỳ, remove 1 phần tử thông qua
giá trị, remove 1 phần tử bất kỳ thông qua vị trí.

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 14


Python Implementation

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 15


Python Implementation

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 16


Python Implementation

© 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences 17

You might also like