Array Flashcards
Provide a description of an array.
An array is a LIST with a FIXED SIZE which contains SIMILAR VALUES.
What can be stored in an array?
Anything, provided they are of the same type.
What is a Parallel Array?
Two or more arrays which…
- Contain the same number of elements.
- Have corresponding values in the same position.
Why are Parallel Arrays useful?
For storing different types of data about the same entity.
What is an alternative to Parallel Arrays?
Storing the related data in a single container object in a single array.
Can an array’s size be changed after instantiation?
No
What is a NUMERICAL INDEX?
An integer which corresponds to an element’s position within an array.
How do we get (access) an element that is stored within an array?
Use a numerical index.
What is the first possible numerical index of an array?
0
What is the last possible numerical index of an array?
array.size - 1
What is a multi-dimensional array and give examples.
An array within an array. 2D array - Pixel image. Many nested arrays - Calendar.
What is the BigO of ACCESSING an element in an Array?
O(1)
How does an array achieve O(1) for access operations?
An array stores each element in contiguous blocks of memory. This allows for fast ACCESS as we can quickly calculate an elements location in memory using the array’s start memory address and adding to it the memory address of the supplied numerical index multiplied by an elements memory size. We can do this as arrays store only a single data type and so we know the size of each element. This is an example of Pointer Arithmetic.
What is the BigO of SEARCHING for an element in an UNSORTED Array?
O(n) as you will have to perform a linear search of (in worst-case scenario) every element in the array.
What is the BigO of SEARCHING for an element in an SORTED Array?
O(log n) using a binary search.