Editing Array

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 2: Line 2:
  
 
In [[functional programming language]]s based upon the [[lambda calculus]], arrays are not fundamental ingredients, as the lambda calculus is not aware of the organization of computer memory. Nevertheless, they may have language support for the sake of efficiency.
 
In [[functional programming language]]s based upon the [[lambda calculus]], arrays are not fundamental ingredients, as the lambda calculus is not aware of the organization of computer memory. Nevertheless, they may have language support for the sake of efficiency.
 
In some programming languages (notably PHP), all arrays are [[Associative array|associative]] by default. An associative array is not a type of array, but a more complex data structure named in analogy to the array.
 
  
 
==Terminology==
 
==Terminology==
Line 9: Line 7:
  
 
A '''segment''' or '''slice''' of an array consists of exactly those values of the array occurring at indices taken from a contiguous range. For example, we may choose the entire array, or we may choose no elements at all, or we may choose the fifth element, or we may choose the ninth, tenth, eleventh, and twelfth elements; all these are considered segments or slices.
 
A '''segment''' or '''slice''' of an array consists of exactly those values of the array occurring at indices taken from a contiguous range. For example, we may choose the entire array, or we may choose no elements at all, or we may choose the fifth element, or we may choose the ninth, tenth, eleventh, and twelfth elements; all these are considered segments or slices.
 
===Indexing convention===
 
The vast majority of arrays encountered in practice are '''zero-indexed''' or '''zero-based'''; that is, they are indexed with consecutive natural numbers starting from zero. This is probably due to the influence of C, in which arrays are formalisms constructed over pointers to their initial elements and indices are merely offsets, so that index 0 represents the initial element itself.
 
 
In theoretical contexts, arrays are again often zero-indexed, but are also often '''one-based''' or '''one-indexed''', that is, indexed with consecutive natural numbers starting from one. The author usually chooses whichever is more convenient.
 
 
Also, note that sometimes a zero-indexed array used in practice is treated as a one-indexed array, and the element at index zero is not used, or it is allowed to exist only for the sake of convenience, ''i.e.'', as a sentinel value, or a buffer against out-of-bounds accesses.
 
  
 
==Address computation==
 
==Address computation==
Line 34: Line 25:
 
But this is cumbersome. Suppose that the guest sitting in the 12th row and the 34th column is randomly selected to win a prize, and we wish to call him out by name. Then we have to compute that his ID is <math>(34-1) + 50\cdot(12-1) = 583</math>, and look up the array element at index 583. It would be much more convenient if we could simply use the ordered pair <math>(34,12)</math> as an index --- not a location along a ''line'', but the coordinates of a point in a ''plane''. The array is being used, after all, to represent a ''table'' or ''matrix'' or ''grid'' of things, not a ''line''.
 
But this is cumbersome. Suppose that the guest sitting in the 12th row and the 34th column is randomly selected to win a prize, and we wish to call him out by name. Then we have to compute that his ID is <math>(34-1) + 50\cdot(12-1) = 583</math>, and look up the array element at index 583. It would be much more convenient if we could simply use the ordered pair <math>(34,12)</math> as an index --- not a location along a ''line'', but the coordinates of a point in a ''plane''. The array is being used, after all, to represent a ''table'' or ''matrix'' or ''grid'' of things, not a ''line''.
  
===First solution===
 
 
This, in fact, is the principle behind a '''multidimensional array''', an array indexed by <math>n</math>-tuples, where <math>n > 1</math>. (Such an array is said to be <math>n</math>-dimensional.) It is not hard to form a bijection between tuples and <math>\mathbb{N}</math>; in this example, we have a two-dimensional array with <math>f(0) = (1,1)</math>, <math>f(1) = (1,2)</math>, ..., <math>f(49) = (1,50)</math>, <math>f(50) = (2,1)</math>, ..., <math>f(99) = (2,50)</math>, ..., <math>f(2450) = (50,1)</math>, ..., <math>f(2499) = (50,50)</math>. The point is that, in order to ease the cognitive burden on the programmer and aid visualization, the details of the address translation should be taken care of by the compiler and hidden from the programmer. Many programming languages support arrays with several dimensions; these are particularly important in [[dynamic programming]]. The size of a multidimensional array is the product of the sizes of the individual sets whose Cartesian product was taken to form the set of tuples as indices for the array. The elements of the tuple need not be all of the same type.
 
This, in fact, is the principle behind a '''multidimensional array''', an array indexed by <math>n</math>-tuples, where <math>n > 1</math>. (Such an array is said to be <math>n</math>-dimensional.) It is not hard to form a bijection between tuples and <math>\mathbb{N}</math>; in this example, we have a two-dimensional array with <math>f(0) = (1,1)</math>, <math>f(1) = (1,2)</math>, ..., <math>f(49) = (1,50)</math>, <math>f(50) = (2,1)</math>, ..., <math>f(99) = (2,50)</math>, ..., <math>f(2450) = (50,1)</math>, ..., <math>f(2499) = (50,50)</math>. The point is that, in order to ease the cognitive burden on the programmer and aid visualization, the details of the address translation should be taken care of by the compiler and hidden from the programmer. Many programming languages support arrays with several dimensions; these are particularly important in [[dynamic programming]]. The size of a multidimensional array is the product of the sizes of the individual sets whose Cartesian product was taken to form the set of tuples as indices for the array. The elements of the tuple need not be all of the same type.
  
===Second solution===
 
Instead of allowing tuples as indices, we could, in fact, simply allow the use of more than one index to an array. A two-dimensional array will be considered as a one-dimensional array of one-dimensional arrays. A three-dimensional array will be considered a one-dimensional array of one-dimensional arrays of one-dimensional arrays. And so on. After all, we did not place any restrictions on the value types of arrays! In the example above, then, each row is viewed as an array of 50 strings, and the whole arrangement is viewed as an array of 50 rows. To access the person in row 12 and column 34, we take the 34th element of the 12th element of the array.
 
 
Programming languages that take this approach to multidimensional arrays generally allow extraction of the lower-dimensional components, thus, the entire first row could be extracted and passed as an argument to a function, for example. In the first solution (tuple indices), these are slices.
 
 
===Note on interpretation===
 
 
(It is important to remember that a multidimensional array is still, in a sense, a line of values sitting in memory, and that the convention of indexing them by tuples is only for convenience and has no real existence within the machine. Accordingly, the interpretation of the array determines its abstract nature, and not ''vice versa''; whether the first element of an ordered pair as the index represents the row or the column is irrelevant as long as one is consistent, and they may represent, in fact, whatever the programmer wants; the first element may identify a star system and the second one of the stars in the system identified by the first, for example, or the interpretation may be totally abstract, with no physical nature.)
 
(It is important to remember that a multidimensional array is still, in a sense, a line of values sitting in memory, and that the convention of indexing them by tuples is only for convenience and has no real existence within the machine. Accordingly, the interpretation of the array determines its abstract nature, and not ''vice versa''; whether the first element of an ordered pair as the index represents the row or the column is irrelevant as long as one is consistent, and they may represent, in fact, whatever the programmer wants; the first element may identify a star system and the second one of the stars in the system identified by the first, for example, or the interpretation may be totally abstract, with no physical nature.)
 
==Mutable and immutable arrays==
 
In imperative programming languages, arrays are typically ''mutable'' by default, which means that each element of an array can be freely treated as a variable with both read and write access. In contrast, ''immutable'' arrays are sometimes encountered, arrays in which all values must be specified as soon as the array is created, and none of the elements may thereafter be modified. Immutable arrays can be used to enforce referential transparency in functional languages, but with the caveat that updating one, which really involves creating a new one that differs in only one element, requires time linear in the size of the array. Nevertheless, functional programming languages usually have support for mutable arrays. In imperative programming languages, there are often ways to create immutable arrays, should the need arise. (Java's strings are immutable by default.)
 
 
==Extensible and fixed arrays==
 
A ''fixed'' array is an array whose size cannot be changed, but is fixed at creation, whereas an ''extensible'' array is an array whose size can be changed after it is created. When the size of an array is changed from <math>n</math> to <math>m</math>, it is understood that the first <math>\min(n,m)</math> elements of the array are left intact.
 
 
Fixed arrays are easy to implement. A problem with implementing extensible arrays is that we cannot always keep the array in the same place. This is because when we first allocate the array, we might not know how large it will become. We cannot allocate an unlimited amount of space for it, so instead we allocate enough space for its initial size. If the array needs to grow, then we cannot simply reserve the next memory address that the array should contain if it were larger, because this block of memory may be in use by another variable or another program. Instead, we need to allocate a new block of memory that is large enough for the extended array, and copy over all the elements. Naively, if we repeatedly extend the array one element at a time, we can force each operation to take linear time, giving <math>O(n^2)</math> time to insert <math>O(n)</math> elements one at a time. This is not an acceptable performance, so another solution is to double the size of allocated memory for the array every time the array grows beyond its current memory allocation; it can be shown that insertions then take place in amortized <math>O(1)</math> time, although some space is wasted.
 
 
==Array operations==
 
Algorithms that work with arrays tend to loop over the elements of the array, considering them one (or a few) at a time. However, there are some operations that are so frequently encountered that they have library support, such as:
 
* ''Fill'': Set every element in the array, or slice thereof, to the same value.
 
* ''Copy'': Copies an array, or slice thereof, to another array or slice thereof, overwriting the values that were there previously, for example, setting the first, second, and third elements of an array to the values of the fifth, sixth, and seventh elements of another array, respectively.
 
* ''Search'': Find the first element in the array that is equal to a given value.
 
* ''Reverse'': Rearrange the elements of the array so their order is the opposite of their original order.
 
* ''Sort'': Rearrange the elements of the array so that each element is less than or equal to the one following it (except the last).
 
* ''Filter'': Create a new array whose elements are exactly those in an original, given array, that satisfy a given unary predicate, without rearranging them.
 
* ''Map'': Apply the same unary function to every element in the array, either replacing it by the result of the function, or creating a new array that contains all the return values of the function, without rearranging them. (An example would be converting a string to uppercase.)
 
* ''Reduce'': Consider each element of the array in turn, passing it to a binary function to update an accumulator, until all elements have been considered, and then return the accumulator. (Examples would be summing the array or finding its maximum element.)
 
 
==Limitations==
 
Suppose that a person cuts into line. How do we update the array of names? The problem is that everybody behind this person will have to have their IDs reassigned to reflect the new structure. That is, when we force an element into a particular location in an array, we have to shift over all the elements to its right, which requires a potentially linear amount of work copying elements. The same is true if someone in the middle of the line or the beginning leaves; all the people after him/her in the line will have to be shifted forward one position (backward in the array). When insertions and deletions are frequent, and most access is sequential, a [[linked list]] may perform better; when insertion, deletion, and random access are all frequent, the use of a [[Binary search tree|tree]] is recommended.
 
 
==Derived data structures==
 
The array can easily be used to implement a [[stack]], [[queue]], [[deque]], or [[binary heap]].
 
 
[[Category:Data structures]]
 

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)