Arrays are the most basic of all data structures. In this article, I will try to explain in simple terms what makes arrays special.
Data structure
Arrays are data of the same type stored in a contiguous space in memory. An array has a fixed size, and cannot grow or shrink.
To resize an array, you would need to create a new array that is larger than the original, and copy all the elements from the original array to the new array. Once the data has been copied, the original array can be freed from memory.
Not all the elements in an array must contain data, but the space in memory for that element is still reserved when an array is created.
You can create arrays that consist of arrays. These two-dimensional arrays can be visualized like a table with rows and columns. In computer memory, each row in the table is stored after the next.
Lookup
To look up a specific index in a one-dimensional array, you multiply the index by the memory size of the data type, and add that number to the memory address of the first element in the array to find the exact location of the array element in memory.
For example, in a 16-bit system, you can store 16-bit integers. Since each address in memory can hold 1 byte (8 bits) of data, a 16-bit integer will occupy two memory addresses. If you want to access the third element (index of 2) in an array of 16-bit integers, you look up the memory address of the array, and add 4 to that address to get the location of the third element.
0x000a <--- Address of 16-bit integer array (first element in array)
0x000c <--- Address of the second element in the array
0x000e <--- Address of the third element in the array
This makes it quick to look up specific indexes in an array.
A lookup in a two-dimensional array requires a bit more complex math because you first have to calculate where the selected row starts in memory, and then where the column is offset from the start of that row, where the data is stored.
Insert / Delete
Because an array has a fixed size, you will encounter a problem when you need to insert or remove data at a specific location within the array. You will then have to move all the elements in the array after the inserted or deleted item.
[1, 2, 3, 4, 5, null] <---- Before insert
[1, 2, 3, null, 4, 5] <---- Making space
[1, 2, 3, 9, 4, 5] <---- After insert
[1, 2, 3, 4, 5, 6] <---- Before delete
[1, 2, 3, 5, 6, null] <---- After delete
This problem makes deleting or inserting items at the beginning and in the middle of an array very slow.
Challenges
Practice makes you better
DSA (Data Structures and Algorithms) can only be learned by using them. I believe the most effective approach to learning DSA is first to understand the theory and then solve challenges.
I encourage you first to solve these challenges by first using a brute force algorithm, then try to solve them with a time and space complexity of less than O(n2).
Here are the problems I solved when first learning about arrays using JavaScript:
- Create an array from scratch based on a CustomArray class
- Push data to the end of the array method
- Pop data off the end of the array method
- Delete data at an index method
- Insert data at an index method
- Reversing an array
- Merging two sorted arrays
- Increasing the size of an array
- Reduce the size of an array
Time and space complexity
Are you unsure what is meant by time and space complexity, or do you need a refresher?