5 List
5 List
<Lecture 5: List>
Sungmin Cha
New York University
09.18.2024
Outline
• Notice
• List
– ArrayList
– Abstracted Data Type (ADT)
– Operations
2
Outline
• Notice
• List
– ArrayList
– Abstracted Data Type (ADT)
– Operations
3
Notice
• HW
– HW1 is posted to our Notion page!
Deadline: 11:59PM 10/06(Sun)
– HW must be completed individually!
– Late submission
A 50% penalty to the score
– I will upload a template code and make a submission slot in
Gradescope after this lecture
• Lab 2– Problems
– I uploaded the solution code and test cases of Problem 2 to Ed
Workspace
4
Notice
• Let’s use ED discussion more
– There are many similar Q&As by email
– Post your question to share the question and answer
5
Outline
• Notice
• List
– ArrayList
– Abstracted Data Type (ADT)
6
Time Complexity
• Asymptotic Running Time
– In other words, it is time complexity
– The primary method for expressing time complexity is Big-O
𝚶(): asymptotic upper bound
Ω(): asymptotic lower bound You will learn them
Θ(): asymptotic tight bound during the Algorithm class
n 𝒏𝟐 𝒏𝟐 − 𝟐𝒏 𝒏𝟐 − 𝟐𝒏 + 𝟏
10 100 80 81
20 400 360 361
50 2,500 2,400 2,401
100 10,000 9,800 9,801
1000 1,000,000 998,000 998,001
8
Time Complexity
• What is the worst-case time complexity (Big-O)?
– [Group Discussion] Example 7:
9
Recursion
• What is recursion?
– Recursion occurs when the definition of a concept or process
depends on a simpler or previous version of itself
From Wikipedia
10
Recursion
• What is recursion?
– Example: Factorial (n!)
𝑛! = 𝑛 × 𝑛 − 1 × 𝑛 − 2 × 𝑛 − 3 × 𝑛 − 4 × … × 2 × 1
(𝑛 − 1)!
𝑛 × 𝑓 𝑛−1 … 𝑛≥1
𝑓 𝑛 =ቊ
1 … 𝑛=1
11
Recursive Function
• Recursion in programming language (JAVA)
– Recursive function refers to a function that calls itself within
its own function
When a recursion function is called, how does the layout of the stack
memory change?
12
Recursive Function
• Understanding the flow of a recursive function
– When a recursive function is called, a copy of the recursive
function is created, and the copy is then executed
– When a recursive function returns, the return value is sent back
to the position where the recursive function was called in the
previous function
Then, the subsequent statements are executed
13
Recursive Function
• The exit condition in recursion
– Setting the exit condition well is important
– Why?
If there is no proper exit condition, the function calls functions infinitely
o stack overflow
14
Outline
• Notice
• List
– ArrayList
– Abstracted Data Type (ADT)
– Operations
15
Overview of this course
• Two parts of this course
1. Preliminary (1-2 weeks)
JAVA programming
Algorithm complexity and recursion.
16
Goal
• Evaluation
– Assignments and labs (recitation) assess these goals
– The goal of the exam is primarily to assess the achievement of
the first goal! 17
List
• Let’s consider we implement a simple program
– Program to store and manage information of 𝑘 users
1. (Integer) Age of 𝑘 individuals
2. (Float) Height of 𝑘 individuals
3. (String) Name of 𝑘 individuals
18
ArrayList
• Using an Array as a List
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example:
Allocate object on the heap Size = 4byte * 10
0 0 0 0 0 0 0 0 0 0 heap
array
stack 100
19
ArrayList
• The memory map of Array Heap memory
1bit
– Example:
…
0x12AB85
4byte
….
4byte
ages = 0x12AB85
…
…
…
20
ArrayList
• The memory map of Array Heap memory
1bit
– Example:
…
0x12AB85
15(binary) 4byte
….
4byte
ages = 0x12AB85
…
…
…
21
ArrayList
• The memory map of Array Heap memory
1bit
– Example:
…
0x12AB85
15(binary) 4byte
….
4byte
ages = 0x12AB85
…
…
…
22
ArrayList
• The memory map of Array Heap memory
1bit
– Example:
…
0x12AB85
15(binary) 4byte
….
ages = 0x12AB85 33(binary) 4byte
…
…
…
23
ArrayList
• What operations are necessary to maintain and manage
this list?
– Add a value to the specific location
– Remove a value in the i-th location
– Get a value in the i-th location
– Find a value
– …, etc.
How can we effectively define and specify
these operations for implementation?
24
ADT
• 1) ADT design for ArrayList
– Example:
27
ADT
• In the user program
28
ADT
• From ADT to the user program
1. Designing ADT
: First, we will design ADT of each data structure
2. Designing JAVA Interface
: we will define JAVA interface of each data structure
(e.g., instructions of homework)
3. Implementing Class
: we will go over the implementation details for some
operations, but the actual coding will be assigned as HW
4. User program
29
ADT of ArrayList
• Designing ADT for ArrayList
31
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Here
32
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Move one position to the right
from the rightmost element
Here
33
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Move one position to the right
from the rightmost element
Here
34
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Move one position to the right
from the rightmost element
Here
35
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
add(2, 34)
numItems = 5
item[0] Insert 34
item[ ] 23 12 34 59 32 9
Here
36
Add()
• Implementation of add(k, x) function
– Example: before and after applying add(2, 34)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Update numItems
add(2, 34)
numItems = 6
item[0]
item[ ] 23 12 34 59 32 9
Here
37
Add()
• [Group Discussion] Defensive programming
– Considering the exception case of add(k,x), what is the general
form of add(k,x)?
38
Add()
• [Group Discussion] Defensive programming
– Considering the exception case of add(k,x), what is the general
form of add(k,x)?
39
Add()
• [Group Discussion] Time complexity
– What is the worst-case time complexity (Big-O)?
Big-O: asymptotic upper bound
40
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
41
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
42
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9 24
Add 24 to item[numItems]
43
Append()
• Implementation of append(x) function
– Example: before and after applying append(24)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
Update numItems
append(24)
numItems = 6
item[0]
item[ ] 23 12 59 32 9 24
44
Append()
• Defensive programming
– Considering the exception case of append(x), what is the
general form of append(x)?
45
Remove()
• [Group Discussion] Implementation of remove(k)
– Example: before and after applying remove(2)
numItems = 5
item[0]
item[ ] 23 12 59 32 9
46
Remove()
• Defensive programming
– Considering the exception case of remove(k), what is the
general form of remove(k)?
47
Remove()
• Time complexity
– What is the worst-case time complexity (Big-O)?
Big-O: asymptotic upper bound
48
Other Operations
• Other operations
– ListInterface of ArrayList
Already introduce
operations, time complexity,
and exceptions
Homework
: Implement operations
using JAVA, what is the
time complexity?
49
ArrayList
• Advantages and disadvantages of ArrayList
– Advantages
1. Simple to implement
2. Data referencing is convenient: it allows quick access anywhere based
on the index value
– Disadvantages
1. The length of the array must be determined initially; if it needs to be
changed, a new allocation of the array is necessary
2. During the process of adding or deleting data, frequent data
movement (copying) occurs
50
ArrayList
• Discussion on the first disadvantage Heap memory
– Can we expand the array? 1bit
…
0x12AB85
15(binary) 4byte
….
ages = 0x12AB85 33(binary) 4byte
…
…
51
Thank you!
E-mail: [email protected]
52