Arrays and amortised cost
1.[2p] An array of 8-byte elements starts at address 4096. At what address does element 250 begin, with 0-based indexing?
An array of 8-byte elements starts at address 4096. At what address does element 250 begin, with 0-based indexing?
2.[2p] Why does reading a[999999] cost the same as reading a[0]?
Why does reading a[999999] cost the same as reading a[0]?
The answer is: The address is computed by arithmetic on the index alone, so no intervening element is examined
The answer is: The address is computed by arithmetic on the index alone, so no intervening element is examined
The answer is: The address is computed by arithmetic on the index alone, so no intervening element is examined
3.[3p] A dynamic array with growth factor 2 starts at capacity 1 and receives 1000 pushes. How many element copies occur in total?
A dynamic array with growth factor 2 starts at capacity 1 and receives 1000 pushes. How many element copies occur in total?
4.[3p] Growing a dynamic array by a fixed 1000 extra slots each time it fills gives an amortised push cost of
Growing a dynamic array by a fixed 1000 extra slots each time it fills gives an amortised push cost of
The answer is: , because the total copying is
The answer is: , because the total copying is
The answer is: , because the total copying is
5.[2p] Amortised analysis is a worst-case statement about a sequence of operations, and involves no assumption about the distribution of inputs.
Amortised analysis is a worst-case statement about a sequence of operations, and involves no assumption about the distribution of inputs.
The answer is: True
6.[2p] With growth factor , about how many element copies does building a dynamic array up to elements cost? Give the answer in millions.
With growth factor , about how many element copies does building a dynamic array up to elements cost? Give the answer in millions.
7.[3p] Match each array operation to its cost on elements.
Match each array operation to its cost on elements.
Read by index
Insert at the front
Push at the end
Delete at position k, order not preserved
constant
constant
linear
amortised constant
Show the answer
Read by index: constant Insert at the front: linear Push at the end: amortised constant Delete at position k, order not preserved: constant
8.[2p] Why does a dynamic array shrink only when its length falls below a quarter of its capacity, rather than a half?
Why does a dynamic array shrink only when its length falls below a quarter of its capacity, rather than a half?
The answer is: Otherwise pushing and popping across the boundary reallocates on every operation
The answer is: Otherwise pushing and popping across the boundary reallocates on every operation
The answer is: Otherwise pushing and popping across the boundary reallocates on every operation
9.[3p] Which are true of raising the growth factor from 2 to 4?
Which are true of raising the growth factor from 2 to 4?
Select all that apply
The answer is: Fewer copies per element, More memory can sit empty just after a growth, The worst-case cost of a single push is still linear