Download presentation
Presentation is loading. Please wait.
Published byBernadette Richard Modified over 6 years ago
1
Fundamentals of Programming II Working with Arrays
Computer Science 112 Fundamentals of Programming II Working with Arrays
2
Collections and Data Structures
Each type of collection uses an underlying data structure to contain its items One common data structure is the array For example, Python’s list type uses an array as the container of its items
3
What Is an Array? An array contains a fixed number of items that can be accessed by position (from 0 to the size of the array minus 1) The type of access is random, meaning that the amount of access time does not vary with the position The memory for an array is a single block of adjacent cells RAM is actually a big array of memory cells
4
Arrays and Computer Memory
1 2 2 1 10 ... ... numbers 3 6 5 2 656 657 658 659 660 3 6 5 Memory address of numbers[2] = base address of numbers + 2 = = 658 2 ... ... 65534 65535 10 9
5
Array vs List The size of an array is fixed when it is created
Items can be accessed or replaced, but not inserted or removed The only operations are str, len, the for loop, and the subscript []
6
Basic Array Operations
Module: from arrays import Array Constructor: a = Array(size = 10, fillValue = None) Length: len(a) Access: a[index] Replacement: a[index] = newItem Iteration: for item in a: size is required, fillValue is optional (None by default) Items can be of any type index must be >= 0 and < len(a)
7
Complications When arrays are used to implement other collections, such as lists, the arrays are not always full, and sometimes they run out of room Any other types of operations, such as insertions and removals, require more programmer and computer effort
8
Physical Size vs Logical Size
When created, arrays have a permanent capacity (number of physical cells) The number of data values (the logical size of an array-based list) may not equal the capacity array Occupied cells
9
Physical Size vs Logical Size
numbers = Array(5) for i in range(4): numbers[i] = i numbers 1 2 3 Physical size = number of cells in array Logical size = number of data values currently stored and used by the program
10
Tracking the Logical Size
numbers = Array(5) numCount = 0 for i in range(4): numbers[i] = i numCount += 1 numbers 1 2 3 numCount 4 Physical size = len(numbers) Logical size = numCount
11
Adding a Datum at the End
numbers = Array(5) numCount = 0 if numCount < len(numbers): numbers[numCount] = datum numCount += 1 numbers 1 2 3 numCount 4 Check for full array before adding a new datum
12
Removing a Datum at the End
if numCount > 0 numCount -= 1 numbers 1 2 3 numCount 4 Check for empty array before removing a datum
13
Replace, Insert, and Delete
A replacement requires no shifting of data A deletion requires shifting data to the left, to close a hole An insertion requires shifting data to the right, to open a hole
14
Replacement numbers 1 2 3 0 1 2 3 4 i 1 newItem numCount 8 4
# Replace the ith item numbers[i] = newItem numbers 1 2 3 i 1 newItem numCount 8 4
15
Replacement numbers 8 2 3 0 1 2 3 4 i 1 newItem numCount 8 4
# Replace the ith item numbers[i] = newItem numbers 8 2 3 i 1 newItem numCount 8 4
16
Deletion Data to shift to the left numbers 8 2 3 0 1 2 3 4 i 1
# Delete the ith item <shift items after i to the left by one position> <decrement logical size> Data to shift to the left numbers 8 2 3 i 1 numCount 4
17
Deletion numbers 2 3 0 1 2 3 4 i 1 numCount 3 # Delete the ith item
<shift items after i to the left by one position> <decrement logical size> numbers 2 3 i 1 numCount 3
18
Deletion numbers 8 2 3 0 1 2 3 4 i 1 j 1 j + 1 numCount 2 4
# Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 8 2 3 i 1 j 1 j + 1 numCount 2 4
19
Deletion numbers 2 2 3 0 1 2 3 4 i 1 j 2 j + 1 numCount 3 4
# Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 2 2 3 i 1 j 2 j + 1 numCount 3 4
20
Deletion numbers 2 3 3 0 1 2 3 4 i 1 j 3 j + 1 numCount 4 4
# Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 2 3 3 i 1 j 3 j + 1 numCount 4 4
21
Deletion numbers 2 3 0 1 2 3 4 i 1 j 3 j + 1 numCount 4 3
# Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 2 3 i 1 j 3 j + 1 numCount 4 3
22
Insertion Data to shift to the right numbers 8 3 4 0 1 2 3 4 i 1
# Insert the ith item <shift items including and after i to the right by one position> <replace item at position i> <increment logical size> Data to shift to the right numbers 8 3 4 i 1 newItem numCount 6 4
23
Insertion numbers 6 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 5
# Insert the ith item <shift items including and after i to the right by one position> <replace item at position i> <increment logical size> numbers 6 8 3 4 i 1 newItem numCount 6 5
24
Insertion numbers 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 4 3
# Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 3 4 i 1 newItem numCount 6 4 j j - 1 4 3
25
Insertion numbers 8 3 4 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 3
# Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 3 4 4 i 1 newItem numCount 6 4 j j - 1 3 2
26
Insertion numbers 8 3 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 2
# Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 3 3 4 i 1 newItem numCount 6 4 j j - 1 2 1
27
Insertion numbers 8 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 1
# Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 8 3 4 i 1 newItem numCount 6 4 j j - 1 1
28
Insertion numbers 6 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 5 j j - 1 1
# Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 6 8 3 4 i 1 newItem numCount 6 5 j j - 1 1
29
Complexity Analysis Replacement, like access, is O(1)
Insertion requires shifting n - i items to the right, O(n) on the average Deletion requires shifting n - i items to the left, O(n) on the average
30
Can We Get More or Less Space?
Python arrays cannot be resized But we can create a new array that’s larger or smaller transfer data from the original array to the new array reset the original array variable to the new array
31
Strategy I: Add One Cell
Increments the length of the array by 1 After the logical size reaches the default physical size, this will result in a resizing each time a new item is added to the array What is the memory complexity of each insertion?
32
Strategy I: Add One Cell
After the default size is reached, each new insertion requires the use of N new memory cells, so the memory and running time complexities are O(n)
33
Strategy II: Double the Length
Increase the length of the array a factor of 2 After the logical size reaches the default physical size, resizings will not occur very often
34
Make the Array Twice as Large
numbers = Array(4) # Then put 3, 2, 6, and 4 in there numbers 3 2 6 4
35
Make the Array Twice as Large
numbers = Array(4) # Then put 3, 2, 6, and 4 in there tempArray = Array(len(numbers) * 2) numbers 3 2 6 4 tempArray
36
Make the Array Twice as Large
numbers = Array(4) # Then put 3, 2, 6, and 4 in there tempArray = Array(len(numbers) * 2) for i in range(len(numbers)): tempArray[i] = numbers[i] numbers 3 2 6 4 tempArray 3 2 6 4
37
Make the Array Twice as Large
numbers = Array(4) # Then put 3, 2, 6, and 4 in there tempArray = Array(len(numbers) * 2) for i in range(len(numbers)): tempArray[i] = numbers[i] numbers = tempArray numbers Off to garbage collector 3 2 6 4 tempArray 3 2 6 4
38
A General Method def resize(array, logicalSize, sizeFactor):
tempArray = Array(round(len(array) * sizeFactor)) for i in range(logicalSize): tempArray[i] = array[i] return tempArray numbers = Array(4, 0) numbers = resize(numbers, 4, 2) # Double the length copy = resize(numbers, 4, 1) # Just a copy copy = resize(numbers, 4, .5) # ½ the length
39
Assignment Creates an Alias
numbers = Array(4) # Then put 3, 2, 6, and 4 in there alias = numbers # An alias copy = resize(numbers, 1) # A real copy numbers 3 2 6 4 alias copy 3 2 6 4
40
Complexity Analysis Resizing, when it occurs, is O(n) in time and space Insertions and deletions are O(n) in time and O(1) in space on the average, because the work of resizing is amortized across the n items
41
Introduction to Linked Structures (Chapter 4)
For Monday Introduction to Linked Structures (Chapter 4)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.