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]?
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
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.
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?
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