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 63: Line 54:
 
* ''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.)
 
* ''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.)
 
* ''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]]
 
[[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)