An array is a chunk of contiguous memory that a program can access by using indices—one index per dimension in the array.
Info
Behind the scenes, the program allocates enough contiguous memory to hold the array's data.
- For one-dimensional arrays, the mapping from array indices to memory entries is simple: index i maps to entry i.
- For two-dimensional arrays, the program can map the array entries in one of two ways:
- In row-major order, the program maps the first row of array entries to the first set of memory locations. It then maps the second row to the set of memory locations after the first and so on..
- In column-major order, the program maps the first column of array entries to the first set of memory locations. It then maps the second column to the second set of memory locations, and so forth.
Normally, how a program maps array entries to memory locations is irrelevant to how a program works,
One-Dimensional Arrays
Finding Items
If the items in the array are not sorted, finding an item is a matter of performing a linear search or exhaustive search.
The linear search run-time algorithm's run time
Finding every item would take a total of
steps. Divide this by the number of searches, , to get the average number of steps to find an item in the array which means that the average run time for finding an item in the array is
Finding Minimum, Maximum, and Average
We cannot avoid looking at every item in the array to find these values, so they have run time
Finding Median
In case of an unsorted array either
-
sort the array and find the middle element
Time complexity
-
use the quick select to find the mid - element
Time complexity
Find mode
In case of unsorted array
-
sort the array and iterate to find the longest contiguous sequence
Time complexity , Time complexity
-
store the count of occurrence using count array or hash table
Time complexity , Space complexity
Inserting Items
Inserting an item at the end of a linear array is easy (time complexity ) if the array has enough space and doesn't need extending.
For inserting anywhere
- Find the position to insert the array
- Move the items to create space
Deleting Items
Removing the item with index k from an array means moving the items that come after position k one position closer to the beginning of the array.
Info
In some cases, it may be possible to flag an entry as unused instead of actually removing it.
This technique can be particularly useful in hash tables, where resizing the array and rebuilding the hash table would be time-consuming.
If flagging many entries as unused, the array could eventually fill up with unused entries. Then, to find an item, would need to examine a lot of empty positions.
At some point, may want to compress the array to remove the empty entries.
Nonzero Lower Bounds
Programming languages require that all arrays use 0 for a lower bound in every dimension. But sometimes it's convenient to treat an array's dimension as if it had nonzero lower bounds.
Higher dimensional array in to a sigle dimension
Can pack an -dimensional into a one-dimensional array in row-major order.
If there are rows and columns, allocate an array with entries.
For an item in a given row and columns to find its index
index = row × <row size> + column- because there are
<row size>items in each row, that means those rows account forr × <row size>items before this one. - if the item's column number is , then there are items before this item in its row numbered Those items take up positions in the values array.
Triangular Arrays
In a triangular array, the values on one side of the diagonal have some default value, such as 0, null, or blank.
- In an upper-triangular array, the real values lie on or above the diagonal.
- In a lower-triangular array, the nondefault values lie on or below the diagonal.
Info
Some applications can save space by using triangular arrays instead of normal rectangular arrays.
For example, a connectivity matrix represents the connections between points in some sort of network.
The array's entry
connected[i, j]is set to true if there is a flight from airportito airportj. If you assume that there is a flight from airportjto airportiwhenever there is a flight from airportito airportj, thenconnected[i, j] = connected[j, i]. In that case, there's no need to store bothconnected[i, j]andconnected[j, i]because they are the same.
it's probably not worth making a triangular array because you would save only entries, which still isn't all that much memory, and working with the array would be harder than using a normal array. However, a triangular array would save about million entries, which begins to add up to real memory savings, so it may be worth making it into a triangular array.
To build a triangular array simply pack the array's values into a one-dimensional array, skipping the entries that should not be included.
To build a triangular array with rows, allocate a one-dimensional array containing items.
To find the index for an entry with row and column
Sparse Arrays
A sparse array lets you save even more space than a triangular array by not representing the missing entries.
One way to implement a sparse array is to make a linked list of linked lists.
-
The first list holds information about rows.
-
Each item in that list points to another linked list holding information about the array's columns for that row.
-
To make it easier to determine when a value is missing from the array, the
ArrayRowobjects are stored in increasing order ofRowNumber. -
the
ArrayEntryobjects are stored in increasing order ofColumnNumber.\