SlideShare a Scribd company logo
Data Structures
Linear List Array Representation
Andres Mendez-Vazquez
May 6, 2015
1 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
2 / 24
Images/cinvestav-
How do we implement the ADT List?
In our first representation will use an array
Use a one-dimensional array element[]:
a b c d e - - - -
0 1 2 3 4 5 6 7 8
The previous array
A representation of L = (a, b, c, d, e) using position i in element[i].
3 / 24
Images/cinvestav-
How do we implement the ADT List?
In our first representation will use an array
Use a one-dimensional array element[]:
a b c d e - - - -
0 1 2 3 4 5 6 7 8
The previous array
A representation of L = (a, b, c, d, e) using position i in element[i].
3 / 24
Images/cinvestav-
Where to map in the array
Right To Left Mapping
- - - - e d c b a
Mapping That Skips Every Other Position
a - b - c - d - e - -
Wrap Around Mapping
d e - - - - - - a b c
4 / 24
Images/cinvestav-
Where to map in the array
Right To Left Mapping
- - - - e d c b a
Mapping That Skips Every Other Position
a - b - c - d - e - -
Wrap Around Mapping
d e - - - - - - a b c
4 / 24
Images/cinvestav-
Where to map in the array
Right To Left Mapping
- - - - e d c b a
Mapping That Skips Every Other Position
a - b - c - d - e - -
Wrap Around Mapping
d e - - - - - - a b c
4 / 24
Images/cinvestav-
Representation Used In Text
Something Notable
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
Thus
1 Put element i of list in element[i].
2 Use a variable size to record current number of elements
5 / 24
Images/cinvestav-
Representation Used In Text
Something Notable
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
Thus
1 Put element i of list in element[i].
2 Use a variable size to record current number of elements
5 / 24
Images/cinvestav-
Code
Our Implementation
p u b l i c c l a s s S i m p l e A r r a y L i s t <Item> implements
L i n e a r L i s t <Item >{
// p r i v a t e elements of implementation
p r o t e c t e d Item element [ ] ;
p r o t e c t e d i n t s i z e ;
p r o t e c t e d f i n a l s t a t i c i n t DEFAULT_SIZE = 10;
// C o n s t r u c t o r s
p u b l i c S i m p l e A r r a y L i s t (){
t h i s . s i z e = 0;
t h i s . element = ( Item [ ] ) new Object [ t h i s . DEFAULT_SIZE ] ;
}
p u b l i c S i m p l e A r r a y L i s t ( i n t NewSize ){
t h i s . s i z e = 0;
t h i s . element = ( Item [ ] ) new Object [ NewSize ] ;
}
6 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Data Type Of Array element[]
First than anything
Data type of list elements is unknown.
Thus, we used the genericity of Object
1 Then, we use element[] to be of data type Object.
2 Then, we cast to the new “Item.”
However
You cannot put elements of primitive data types (int, float, double, char,
etc.) into our linear lists.
7 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
8 / 24
Images/cinvestav-
Operations: Add
Add/Remove an element
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
add(1,g)
a g b c d e - - -
0 1 2 3 4 5 6 7 8
Size=6
9 / 24
Images/cinvestav-
Operations: Add
Add/Remove an element
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
add(1,g)
a g b c d e - - -
0 1 2 3 4 5 6 7 8
Size=6
9 / 24
Images/cinvestav-
Code
Our Implementation
p u b l i c void add ( i n t index , Item myobject ){
// I n i t i a l V a r i a b l e s
i n t i ;
// Always check f o r p o s s i b l e e r r o r s
i f ( t h i s . s i z e == element . len ght ){
System . out . p r i n t l n ( " L i s t ␣ does ␣ not ␣ have ␣ space " ) ;
System . e x i t ( 0 ) ;
}
i f ( index <0 | | index >t h i s . s i z e ){
System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ;
System . e x i t ( 0 ) ;
}
// S h i f t p o s t i i o n s as n e c e s s a r y
f o r ( i = t h i s . s i z e ; i >index ; i −−)
element [ i +1]=element [ i ] ;
// copy element i n t o c o n t a i n e r
element [ i +1]=myobject ;
}
10 / 24
Images/cinvestav-
Operations: Get
Here
We have
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
get(3)
It will return “d”.
The code is simple
11 / 24
Images/cinvestav-
Operations: Get
Here
We have
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
get(3)
It will return “d”.
The code is simple
11 / 24
Images/cinvestav-
Operations: Get
Here
We have
a b c d e - - - -
0 1 2 3 4 5 6 7 8
Size=5
get(3)
It will return “d”.
The code is simple
p u b l i c Item get ( i n t index ){
// Check always
i f ( t h i s . s i z e == 0) r e t u r n n u l l ;
i f ( index <0 | | index >t h i s . s i z e −1){
System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ;
System . e x i t ( 0 ) ;
}
r e t u r n element [ index ]
}
11 / 24
Images/cinvestav-
You can implement the rest
Yes
Part of your homework!!!
12 / 24
Images/cinvestav-
Now, we have a common problem
Because
We do not know how many elements will be stored at the list.
We use
An initial length and dynamically increase the size as needed.
13 / 24
Images/cinvestav-
Now, we have a common problem
Because
We do not know how many elements will be stored at the list.
We use
An initial length and dynamically increase the size as needed.
13 / 24
Images/cinvestav-
Example
Example
Length of array element[] is 6:
a b c d e f
First create a new and larger array
NewArray = (Item[]) new Object[12]
- - - - - - - - - - - -
Now copy the new elements into the new array!!!
System.arraycopy(element, 0, newArray, 0, element.length);
a b c d e f - - - - - -
14 / 24
Images/cinvestav-
Example
Example
Length of array element[] is 6:
a b c d e f
First create a new and larger array
NewArray = (Item[]) new Object[12]
- - - - - - - - - - - -
Now copy the new elements into the new array!!!
System.arraycopy(element, 0, newArray, 0, element.length);
a b c d e f - - - - - -
14 / 24
Images/cinvestav-
Example
Example
Length of array element[] is 6:
a b c d e f
First create a new and larger array
NewArray = (Item[]) new Object[12]
- - - - - - - - - - - -
Now copy the new elements into the new array!!!
System.arraycopy(element, 0, newArray, 0, element.length);
a b c d e f - - - - - -
14 / 24
Images/cinvestav-
Example
Finally, rename new array
element = NewArray;
element[0]−→ a b c d e f - - - - - -
15 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
16 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
First Attempt
What if you use the following policy?
At least 1 more than current array length.
Thus
What would be the cost of all operations?
Generate a new array.
Copy all the items to it.
insert the new element at the end.
17 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
1 + 2 + 3 + · · · + n =
n (n + 1)
2
= O n2
(1)
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
What if we do n insertions?
We finish with something like this
1 First Insertion: Creation of the List ⇒ Cost = 1.
2 Second Insertion Cost = 2.
3 Third Insertion Cost = 3
4 etc!!!
Thus, we have
1 + 2 + 3 + · · · + n =
n (n + 1)
2
= O n2
(1)
Ok
Not a good idea!!!
18 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
19 / 24
Images/cinvestav-
A better strategy
Dynamic Array
To avoid incurring the cost of resizing many times, dynamic arrays resize
by an amount a.
In our example we double the size, a = 2
20 / 24
Images/cinvestav-
A better strategy
Dynamic Array
To avoid incurring the cost of resizing many times, dynamic arrays resize
by an amount a.
In our example we double the size, a = 2
Item NewArray [ ] ;
i f ( t h i s . s i z e == element . len ght ){
// Resize the c a p a c i t y
NewArray = ( Item [ ] ) new Object [ 2*this.size ]
f o r ( i n t i =0; i < s i z e ; i ++){
NewArray [ i ]= element [ i ] ;
}
element = NewArray ;
}
20 / 24
Images/cinvestav-
Space Complexity
Every time an insertion triggers a doubling of the array
Thus, space wasted in the new array:
Space Wasted = Old Lenght − 1 (2)
Remember: We double the array and insert!!!
Thus, the average space wasted is
Θ (n) (3)
21 / 24
Images/cinvestav-
Space Complexity
Every time an insertion triggers a doubling of the array
Thus, space wasted in the new array:
Space Wasted = Old Lenght − 1 (2)
Remember: We double the array and insert!!!
Thus, the average space wasted is
Θ (n) (3)
21 / 24
Images/cinvestav-
For example, we have the following
A trade-off between time and space
You have and average time for insertion is 2
2−1 .
In addition, an upper bound for the wasted cells in the array is
(2 − 1) n − 1 = n − 1.
Actually a more general term of expansion, a
Thus, we have:
Average time for insertion is a
a−1 .
An upper bound for the wasted cells in the array is
(a − 1) n − 1 = an − n − 1.
Different languages use different values
Java, a = 3
2 .
Python, a = 9
8
22 / 24
Images/cinvestav-
For example, we have the following
A trade-off between time and space
You have and average time for insertion is 2
2−1 .
In addition, an upper bound for the wasted cells in the array is
(2 − 1) n − 1 = n − 1.
Actually a more general term of expansion, a
Thus, we have:
Average time for insertion is a
a−1 .
An upper bound for the wasted cells in the array is
(a − 1) n − 1 = an − n − 1.
Different languages use different values
Java, a = 3
2 .
Python, a = 9
8
22 / 24
Images/cinvestav-
For example, we have the following
A trade-off between time and space
You have and average time for insertion is 2
2−1 .
In addition, an upper bound for the wasted cells in the array is
(2 − 1) n − 1 = n − 1.
Actually a more general term of expansion, a
Thus, we have:
Average time for insertion is a
a−1 .
An upper bound for the wasted cells in the array is
(a − 1) n − 1 = an − n − 1.
Different languages use different values
Java, a = 3
2 .
Python, a = 9
8
22 / 24
Images/cinvestav-
Outline
1 Linear List Array Representation
Operations in Array List
2 Dynamic Arrays
How much we need to expand it...
A Strategy for it
Analysis
23 / 24
Images/cinvestav-
We have the following final analysis
Amortized Analysis Vs. Classic
Array Classic Analysis Dynamic Array
Amortized Analysis
Indexing O (1) O (1)
Search O (n) O (n)
Add/Remove O (n) O (n)
Space Complexity O (n) O (n)
24 / 24

More Related Content

PDF
Preparation Data Structures 01 introduction
PDF
Preparation Data Structures 05 chain linear_list
PDF
Preparation Data Structures 06 arrays representation
PDF
Preparation Data Structures 08 queues
PDF
Preparation Data Structures 03 abstract data_types
PDF
Preparation Data Structures 10 trees
PDF
Preparation Data Structures 11 graphs
PPT
Chapter 5 ds
Preparation Data Structures 01 introduction
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 06 arrays representation
Preparation Data Structures 08 queues
Preparation Data Structures 03 abstract data_types
Preparation Data Structures 10 trees
Preparation Data Structures 11 graphs
Chapter 5 ds

What's hot (20)

PPT
Chapter 4 ds
PPTX
Data structures in c#
PPTX
Data Structures - Lecture 3 [Arrays]
PPTX
C# Arrays
PPT
Arrays and structures
PPTX
17. Trees and Tree Like Structures
PPTX
PPT
Data Structure In C#
PPT
Chapter 7 ds
PPTX
7array in c#
PPTX
Arrays in Data Structure and Algorithm
PDF
PPT
Arrays in C++
PPT
PPT
Java Arrays
PDF
computer notes - Priority queue
PDF
Java Arrays
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Set data structure
PDF
Array data structure
Chapter 4 ds
Data structures in c#
Data Structures - Lecture 3 [Arrays]
C# Arrays
Arrays and structures
17. Trees and Tree Like Structures
Data Structure In C#
Chapter 7 ds
7array in c#
Arrays in Data Structure and Algorithm
Arrays in C++
Java Arrays
computer notes - Priority queue
Java Arrays
Arrays In Python | Python Array Operations | Edureka
Set data structure
Array data structure
Ad

Similar to Preparation Data Structures 04 array linear_list (20)

PPT
java detailed aadvanced jaavaaaaa2-3.ppt
PDF
An Introduction to Programming in Java: Arrays
PPT
lec6.ppt
PPT
Array
PPT
Engineering lecture ppt by venay magen
PPTX
Data Structures - Array presentation .pptx
PDF
M v bramhananda reddy dsa complete notes
PPTX
arrays-120712074248-phpapp01
PPTX
Arrays
PPTX
Arrays and Strings engineering education
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPTX
U2.pptx Advanced Data Structures and Algorithms
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
PDF
01A - Greatest Hits of CS111 Data structure and algorithm
PPTX
unit 2.pptx
PPTX
Data Structure Introduction- Arrays, Matrix, Linked List
PPTX
Basic of array and data structure, data structure basics, array, address calc...
PPTX
Data Structures and Agorithm: DS 02 Array List.pptx
PPTX
DSA Unit II array.pptx
PDF
LectureNotes-05-DSA
java detailed aadvanced jaavaaaaa2-3.ppt
An Introduction to Programming in Java: Arrays
lec6.ppt
Array
Engineering lecture ppt by venay magen
Data Structures - Array presentation .pptx
M v bramhananda reddy dsa complete notes
arrays-120712074248-phpapp01
Arrays
Arrays and Strings engineering education
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
U2.pptx Advanced Data Structures and Algorithms
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
01A - Greatest Hits of CS111 Data structure and algorithm
unit 2.pptx
Data Structure Introduction- Arrays, Matrix, Linked List
Basic of array and data structure, data structure basics, array, address calc...
Data Structures and Agorithm: DS 02 Array List.pptx
DSA Unit II array.pptx
LectureNotes-05-DSA
Ad

More from Andres Mendez-Vazquez (20)

PDF
2.03 bayesian estimation
PDF
05 linear transformations
PDF
01.04 orthonormal basis_eigen_vectors
PDF
01.03 squared matrices_and_other_issues
PDF
01.02 linear equations
PDF
01.01 vector spaces
PDF
06 recurrent neural_networks
PDF
05 backpropagation automatic_differentiation
PDF
Zetta global
PDF
01 Introduction to Neural Networks and Deep Learning
PDF
25 introduction reinforcement_learning
PDF
Neural Networks and Deep Learning Syllabus
PDF
Introduction to artificial_intelligence_syllabus
PDF
Ideas 09 22_2018
PDF
Ideas about a Bachelor in Machine Learning/Data Sciences
PDF
Analysis of Algorithms Syllabus
PDF
20 k-means, k-center, k-meoids and variations
PDF
18.1 combining models
PDF
17 vapnik chervonenkis dimension
PDF
A basic introduction to learning
2.03 bayesian estimation
05 linear transformations
01.04 orthonormal basis_eigen_vectors
01.03 squared matrices_and_other_issues
01.02 linear equations
01.01 vector spaces
06 recurrent neural_networks
05 backpropagation automatic_differentiation
Zetta global
01 Introduction to Neural Networks and Deep Learning
25 introduction reinforcement_learning
Neural Networks and Deep Learning Syllabus
Introduction to artificial_intelligence_syllabus
Ideas 09 22_2018
Ideas about a Bachelor in Machine Learning/Data Sciences
Analysis of Algorithms Syllabus
20 k-means, k-center, k-meoids and variations
18.1 combining models
17 vapnik chervonenkis dimension
A basic introduction to learning

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Institutional Correction lecture only . . .
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
GDM (1) (1).pptx small presentation for students
Microbial disease of the cardiovascular and lymphatic systems
2.FourierTransform-ShortQuestionswithAnswers.pdf
A systematic review of self-coping strategies used by university students to ...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Microbial diseases, their pathogenesis and prophylaxis
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Institutional Correction lecture only . . .
Abdominal Access Techniques with Prof. Dr. R K Mishra
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Preparation Data Structures 04 array linear_list

  • 1. Data Structures Linear List Array Representation Andres Mendez-Vazquez May 6, 2015 1 / 24
  • 2. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 2 / 24
  • 3. Images/cinvestav- How do we implement the ADT List? In our first representation will use an array Use a one-dimensional array element[]: a b c d e - - - - 0 1 2 3 4 5 6 7 8 The previous array A representation of L = (a, b, c, d, e) using position i in element[i]. 3 / 24
  • 4. Images/cinvestav- How do we implement the ADT List? In our first representation will use an array Use a one-dimensional array element[]: a b c d e - - - - 0 1 2 3 4 5 6 7 8 The previous array A representation of L = (a, b, c, d, e) using position i in element[i]. 3 / 24
  • 5. Images/cinvestav- Where to map in the array Right To Left Mapping - - - - e d c b a Mapping That Skips Every Other Position a - b - c - d - e - - Wrap Around Mapping d e - - - - - - a b c 4 / 24
  • 6. Images/cinvestav- Where to map in the array Right To Left Mapping - - - - e d c b a Mapping That Skips Every Other Position a - b - c - d - e - - Wrap Around Mapping d e - - - - - - a b c 4 / 24
  • 7. Images/cinvestav- Where to map in the array Right To Left Mapping - - - - e d c b a Mapping That Skips Every Other Position a - b - c - d - e - - Wrap Around Mapping d e - - - - - - a b c 4 / 24
  • 8. Images/cinvestav- Representation Used In Text Something Notable a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 Thus 1 Put element i of list in element[i]. 2 Use a variable size to record current number of elements 5 / 24
  • 9. Images/cinvestav- Representation Used In Text Something Notable a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 Thus 1 Put element i of list in element[i]. 2 Use a variable size to record current number of elements 5 / 24
  • 10. Images/cinvestav- Code Our Implementation p u b l i c c l a s s S i m p l e A r r a y L i s t <Item> implements L i n e a r L i s t <Item >{ // p r i v a t e elements of implementation p r o t e c t e d Item element [ ] ; p r o t e c t e d i n t s i z e ; p r o t e c t e d f i n a l s t a t i c i n t DEFAULT_SIZE = 10; // C o n s t r u c t o r s p u b l i c S i m p l e A r r a y L i s t (){ t h i s . s i z e = 0; t h i s . element = ( Item [ ] ) new Object [ t h i s . DEFAULT_SIZE ] ; } p u b l i c S i m p l e A r r a y L i s t ( i n t NewSize ){ t h i s . s i z e = 0; t h i s . element = ( Item [ ] ) new Object [ NewSize ] ; } 6 / 24
  • 11. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 12. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 13. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 14. Images/cinvestav- Data Type Of Array element[] First than anything Data type of list elements is unknown. Thus, we used the genericity of Object 1 Then, we use element[] to be of data type Object. 2 Then, we cast to the new “Item.” However You cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. 7 / 24
  • 15. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 8 / 24
  • 16. Images/cinvestav- Operations: Add Add/Remove an element a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 add(1,g) a g b c d e - - - 0 1 2 3 4 5 6 7 8 Size=6 9 / 24
  • 17. Images/cinvestav- Operations: Add Add/Remove an element a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 add(1,g) a g b c d e - - - 0 1 2 3 4 5 6 7 8 Size=6 9 / 24
  • 18. Images/cinvestav- Code Our Implementation p u b l i c void add ( i n t index , Item myobject ){ // I n i t i a l V a r i a b l e s i n t i ; // Always check f o r p o s s i b l e e r r o r s i f ( t h i s . s i z e == element . len ght ){ System . out . p r i n t l n ( " L i s t ␣ does ␣ not ␣ have ␣ space " ) ; System . e x i t ( 0 ) ; } i f ( index <0 | | index >t h i s . s i z e ){ System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ; System . e x i t ( 0 ) ; } // S h i f t p o s t i i o n s as n e c e s s a r y f o r ( i = t h i s . s i z e ; i >index ; i −−) element [ i +1]=element [ i ] ; // copy element i n t o c o n t a i n e r element [ i +1]=myobject ; } 10 / 24
  • 19. Images/cinvestav- Operations: Get Here We have a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 get(3) It will return “d”. The code is simple 11 / 24
  • 20. Images/cinvestav- Operations: Get Here We have a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 get(3) It will return “d”. The code is simple 11 / 24
  • 21. Images/cinvestav- Operations: Get Here We have a b c d e - - - - 0 1 2 3 4 5 6 7 8 Size=5 get(3) It will return “d”. The code is simple p u b l i c Item get ( i n t index ){ // Check always i f ( t h i s . s i z e == 0) r e t u r n n u l l ; i f ( index <0 | | index >t h i s . s i z e −1){ System . out . p r i n t l n ( " Index ␣ out ␣ of ␣bound " ) ; System . e x i t ( 0 ) ; } r e t u r n element [ index ] } 11 / 24
  • 22. Images/cinvestav- You can implement the rest Yes Part of your homework!!! 12 / 24
  • 23. Images/cinvestav- Now, we have a common problem Because We do not know how many elements will be stored at the list. We use An initial length and dynamically increase the size as needed. 13 / 24
  • 24. Images/cinvestav- Now, we have a common problem Because We do not know how many elements will be stored at the list. We use An initial length and dynamically increase the size as needed. 13 / 24
  • 25. Images/cinvestav- Example Example Length of array element[] is 6: a b c d e f First create a new and larger array NewArray = (Item[]) new Object[12] - - - - - - - - - - - - Now copy the new elements into the new array!!! System.arraycopy(element, 0, newArray, 0, element.length); a b c d e f - - - - - - 14 / 24
  • 26. Images/cinvestav- Example Example Length of array element[] is 6: a b c d e f First create a new and larger array NewArray = (Item[]) new Object[12] - - - - - - - - - - - - Now copy the new elements into the new array!!! System.arraycopy(element, 0, newArray, 0, element.length); a b c d e f - - - - - - 14 / 24
  • 27. Images/cinvestav- Example Example Length of array element[] is 6: a b c d e f First create a new and larger array NewArray = (Item[]) new Object[12] - - - - - - - - - - - - Now copy the new elements into the new array!!! System.arraycopy(element, 0, newArray, 0, element.length); a b c d e f - - - - - - 14 / 24
  • 28. Images/cinvestav- Example Finally, rename new array element = NewArray; element[0]−→ a b c d e f - - - - - - 15 / 24
  • 29. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 16 / 24
  • 30. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 31. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 32. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 33. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 34. Images/cinvestav- First Attempt What if you use the following policy? At least 1 more than current array length. Thus What would be the cost of all operations? Generate a new array. Copy all the items to it. insert the new element at the end. 17 / 24
  • 35. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 36. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 37. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 38. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have Ok Not a good idea!!! 18 / 24
  • 39. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have 1 + 2 + 3 + · · · + n = n (n + 1) 2 = O n2 (1) Ok Not a good idea!!! 18 / 24
  • 40. Images/cinvestav- What if we do n insertions? We finish with something like this 1 First Insertion: Creation of the List ⇒ Cost = 1. 2 Second Insertion Cost = 2. 3 Third Insertion Cost = 3 4 etc!!! Thus, we have 1 + 2 + 3 + · · · + n = n (n + 1) 2 = O n2 (1) Ok Not a good idea!!! 18 / 24
  • 41. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 19 / 24
  • 42. Images/cinvestav- A better strategy Dynamic Array To avoid incurring the cost of resizing many times, dynamic arrays resize by an amount a. In our example we double the size, a = 2 20 / 24
  • 43. Images/cinvestav- A better strategy Dynamic Array To avoid incurring the cost of resizing many times, dynamic arrays resize by an amount a. In our example we double the size, a = 2 Item NewArray [ ] ; i f ( t h i s . s i z e == element . len ght ){ // Resize the c a p a c i t y NewArray = ( Item [ ] ) new Object [ 2*this.size ] f o r ( i n t i =0; i < s i z e ; i ++){ NewArray [ i ]= element [ i ] ; } element = NewArray ; } 20 / 24
  • 44. Images/cinvestav- Space Complexity Every time an insertion triggers a doubling of the array Thus, space wasted in the new array: Space Wasted = Old Lenght − 1 (2) Remember: We double the array and insert!!! Thus, the average space wasted is Θ (n) (3) 21 / 24
  • 45. Images/cinvestav- Space Complexity Every time an insertion triggers a doubling of the array Thus, space wasted in the new array: Space Wasted = Old Lenght − 1 (2) Remember: We double the array and insert!!! Thus, the average space wasted is Θ (n) (3) 21 / 24
  • 46. Images/cinvestav- For example, we have the following A trade-off between time and space You have and average time for insertion is 2 2−1 . In addition, an upper bound for the wasted cells in the array is (2 − 1) n − 1 = n − 1. Actually a more general term of expansion, a Thus, we have: Average time for insertion is a a−1 . An upper bound for the wasted cells in the array is (a − 1) n − 1 = an − n − 1. Different languages use different values Java, a = 3 2 . Python, a = 9 8 22 / 24
  • 47. Images/cinvestav- For example, we have the following A trade-off between time and space You have and average time for insertion is 2 2−1 . In addition, an upper bound for the wasted cells in the array is (2 − 1) n − 1 = n − 1. Actually a more general term of expansion, a Thus, we have: Average time for insertion is a a−1 . An upper bound for the wasted cells in the array is (a − 1) n − 1 = an − n − 1. Different languages use different values Java, a = 3 2 . Python, a = 9 8 22 / 24
  • 48. Images/cinvestav- For example, we have the following A trade-off between time and space You have and average time for insertion is 2 2−1 . In addition, an upper bound for the wasted cells in the array is (2 − 1) n − 1 = n − 1. Actually a more general term of expansion, a Thus, we have: Average time for insertion is a a−1 . An upper bound for the wasted cells in the array is (a − 1) n − 1 = an − n − 1. Different languages use different values Java, a = 3 2 . Python, a = 9 8 22 / 24
  • 49. Images/cinvestav- Outline 1 Linear List Array Representation Operations in Array List 2 Dynamic Arrays How much we need to expand it... A Strategy for it Analysis 23 / 24
  • 50. Images/cinvestav- We have the following final analysis Amortized Analysis Vs. Classic Array Classic Analysis Dynamic Array Amortized Analysis Indexing O (1) O (1) Search O (n) O (n) Add/Remove O (n) O (n) Space Complexity O (n) O (n) 24 / 24