Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
82 views

Array (Data Structure)

This article provides an overview of arrays as a data structure in computer science. It defines an array as a collection of elements each identified by an index, with the position of each element computed by a mathematical formula from its index. Arrays store elements contiguously in memory, allowing fast access. Common applications include implementing vectors, matrices, tables and other data structures. The article discusses one-dimensional and multi-dimensional arrays, addressing formulas, efficiency, and applications in programming languages.

Uploaded by

R Continue
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Array (Data Structure)

This article provides an overview of arrays as a data structure in computer science. It defines an array as a collection of elements each identified by an index, with the position of each element computed by a mathematical formula from its index. Arrays store elements contiguously in memory, allowing fast access. Common applications include implementing vectors, matrices, tables and other data structures. The article discusses one-dimensional and multi-dimensional arrays, addressing formulas, efficiency, and applications in programming languages.

Uploaded by

R Continue
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Array (data structure)

From Wikipedia, the free encyclopedia


Jump to navigation
Jump to search
This article is about the byte-layout-level structure. For the abstract data type, see Array data
type. For other types of arrays, see Array.

This article
needs
additional
citations for
verification.
Please help
improve this
article by
adding
citations to
reliable
sources.
Unsourced
material may
be
challenged
and
removed.
Find sources:
"Array" data
structure –
news ·
newspapers ·
books · scholar
· JSTOR
(September
2008) (Learn
how and when
to remove this
template
message)

In computer science, an array is a data structure consisting of a collection of elements (values


or variables), each identified by at least one array index or key. An array is stored such that the
[1][2][3]
position of each element can be computed from its index tuple by a mathematical formula.
The simplest type of data structure is a linear array, also called one-dimensional array.

For example, an array of ten 32-bit (4-byte) integer variables, with indices 0 through 9, may be
stored as ten words at memory addresses 2000, 2004, 2008, ..., 2036, (in hexadecimal: 0x7D0,
[4]
0x7D4, 0x7D8, ..., 0x7F4) so that the element with index i has the address 2000 + (i × 4).
The memory address of the first element of an array is called first address, foundation address,
or base address.

Because the mathematical concept of a matrix can be represented as a two-dimensional grid,


two-dimensional arrays are also sometimes called "matrices". In some cases the term "vector"
is used in computing to refer to an array, although tuples rather than vectors are the more
mathematically correct equivalent. Tables are often implemented in the form of arrays,
especially lookup tables; the word "table" is sometimes used as a synonym of array.

Arrays are among the oldest and most important data structures, and are used by almost every
program. They are also used to implement many other data structures, such as lists and strings.
They effectively exploit the addressing logic of computers. In most modern computers and many
external storage devices, the memory is a one-dimensional array of words, whose indices are
their addresses. Processors, especially vector processors, are often optimized for array
operations.

Arrays are useful mostly because the element indices can be computed at run time. Among
other things, this feature allows a single iterative statement to process arbitrarily many elements
of an array. For that reason, the elements of an array data structure are required to have the
same size and should use the same data representation. The set of valid index tuples and the
[3][5]
addresses of the elements (and hence the element addressing formula) are usually, but not
[2]
always, fixed while the array is in use.

The term "array" may also refer to an array data type, a kind of data type provided by most high-
level programming languages that consists of a collection of values or variables that can be
selected by one or more indices computed at run-time. Array types are often implemented by
array structures; however, in some languages they may be implemented by hash tables, linked
lists, search trees, or other data structures.
The term is also used, especially in the description of algorithms, to mean associative array or
"abstract array", a theoretical computer science model (an abstract data type or ADT) intended
to capture the essential properties of arrays.

Contents

● 1
● History

● 2
● Applications

● 3
● Element identifier and addressing formulas
○ 3.1
○ One-dimensional arrays

○ 3.2
○ Multidimensional arrays

○ 3.3
○ Dope vectors

○ 3.4
○ Compact layouts

○ 3.5
○ Resizing

○ 3.6
○ Non-linear formulas

● 4
● Efficiency
○ 4.1
○ Comparison with other data structures

● 5
● Dimension

● 6
● See also

● 7
● References

● 8
● External links

History[edit]
The first digital computers used machine-language programming to set up and access array
structures for data tables, vector and matrix computations, and for many other purposes. John
von Neumann wrote the first array-sorting program (merge sort) in 1945, during the building of
[6]p. 159
the first stored-program computer. Array indexing was originally done by self-modifying
code, and later using index registers and indirect addressing. Some mainframes designed in the
1960s, such as the Burroughs B5000 and its successors, used memory segmentation to
[7]
perform index-bounds checking in hardware.

Assembly languages generally have no special support for arrays, other than what the machine
itself provides. The earliest high-level programming languages, including FORTRAN (1957),
Lisp (1958), COBOL (1960), and ALGOL 60 (1960), had support for multi-dimensional arrays,
and so has C (1972). In C++ (1983), class templates exist for multi-dimensional arrays whose
[3][5] [2]
dimension is fixed at runtime as well as for runtime-flexible arrays.

Applications[edit]
Arrays are used to implement mathematical vectors and matrices, as well as other kinds of
rectangular tables. Many databases, small and large, consist of (or include) one-dimensional
arrays whose elements are records.

Arrays are used to implement other data structures, such as lists, heaps, hash tables, deques,
queues, stacks, strings, and VLists. Array-based implementations of other data structures are
frequently simple and space-efficient (implicit data structures), requiring little space overhead,
but may have poor space complexity, particularly when modified, compared to tree-based data
structures (compare a sorted array to a search tree).

One or more large arrays are sometimes used to emulate in-program dynamic memory
allocation, particularly memory pool allocation. Historically, this has sometimes been the only
way to allocate "dynamic memory" portably.

Arrays can be used to determine partial or complete control flow in programs, as a compact
alternative to (otherwise repetitive) multiple IF statements. They are known in this context as
control tables and are used in conjunction with a purpose built interpreter whose control flow is
altered according to values contained in the array. The array may contain subroutine pointers
(or relative subroutine numbers that can be acted upon by SWITCH statements) that direct the
path of the execution.

Element identifier and addressing formulas[edit]


When data objects are stored in an array, individual objects are selected by an index that is
usually a non-negative scalar integer. Indexes are also called subscripts. An index maps the
array value to a stored object.

There are three ways in which the elements of an array can be indexed:

0 (zero-based indexing)

[8]
The first element of the array is indexed by subscript of 0.

1 (one-based indexing)

The first element of the array is indexed by subscript of 1.

n (n-based indexing)

The base index of an array can be freely chosen. Usually programming languages allowing
n-based indexing also allow negative index values and other scalar data types like
enumerations, or characters may be used as an array index.

Using zero based indexing is the design choice of many influential programming languages,
including C, Java and Lisp. This leads to simpler implementation where the subscript refers to
an offset from the starting position of an array, so the first element has an offset of zero.

Arrays can have multiple dimensions, thus it is not uncommon to access an array using multiple
indices. For example, a two-dimensional array A with three rows and four columns might provide
access to the element at the 2nd row and 4th column by the expression A[1][3] in the case of
a zero-based indexing system. Thus two indices are used for a two-dimensional array, three for
a three-dimensional array, and n for an n-dimensional array.

The number of indices needed to specify an element is called the dimension, dimensionality, or
rank of the array.
In standard arrays, each index is restricted to a certain range of consecutive integers (or
consecutive values of some enumerated type), and the address of an element is computed by a
"linear" formula on the indices.

You might also like