Arrays & Strings Flashcards
Amortized insertion runtime
O(1)
Why is insertion runtime amortized?
Doubling the size of an array happens, but the next time it happens won’t be for a while. Which describes the insertion. It will have to perform O(n) operations to insert, but the next time it happens won’t be for a while.
Why is concatenation bad in languages that use it on strings?
Because a new string is created, then each character is copied over. i.e. First iteration copies x characters, second iteration copies 2x characters
What is the runtime of string concatenation?
O(n^2)
Are strings mutable in Java?
No, strings cannot be changed.
What is an array?
contiguous place in memory that holds items of equal size and is indexed by integers
Access of an array?
Constant O(1) : possible through index
What is runtime of writing to an array?
Constant O(1) : possible through index
How is an array constant access?
The value is found via this equation (memory_address + element_size x (index - first_index) done in the compiler
Memory of an array?
O(n) space
Delete at index of array? Not at end
O(n) to move all elements in array
Insert at index? Not at end
O(n) to shift all elements in array