Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

3.1 The Array Structure

At the hardware level, most computer architectures provide a mechanism for creating and using one-dimensional arrays. A one-dimensional array, as illustrated in Figure 3.1.1, is composed of multiple sequential elements stored in contiguous bytes of memory and allows for random access to the individual elements.

The array structure is another example of a mutable indexed ordered sequence in which the elements are arranged in linear order from front to back and the elements of the sequence are accessible by position. As you may remember, Python provides several indexed sequences, two immutable indexed sequences, strings and tuples, and one mutable indexed sequence, the list.

The entire contents of an array are identified by a single name. Individual elements within the array can be accessed directly by specifying an integer subscript or index value, which indicates an offset from the start of the array. This is similar to the mathematics notation xi , which allows for multiple variables of the same name. The difference is that programming languages typically use square brackets following the array name to specify the subscript, x[i].

Figure 3.1.1: A sample 1-D array consisting of 11 elements.

The Array ADT

The array structure is commonly found in most programming languages as a primitive type, but Python only provides the list structure for creating mutable sequences. We can define the Array ADT to represent a one-dimensional array for use in Python that works similarly to arrays found in other languages. It will be used throughout the text when an array structure is required.

ADT Definition

The Array ADT

A one-dimensional array is a collection of contiguous elements in which individual elements are identified by a unique integer subscript starting with zero. Once an array is created, its size cannot be changed.

  • Array(size)

    Creates a one-dimensional array consisting of size elements with each element initially set to None. size must be greater than zero.

  • length()

    Returns the length or number of elements in the array.

  • getitem(index)

    Returns the value stored in the array at element position index. The index argument must be within the valid range. Accessed using the subscript operator.

  • setitem(index,

    Modifies the contents of the array element at position index to contain value. The index must be within the valid range. Accessed using the subscript operator.

  • clear(value)

    Clears the array by setting every element to value.

Some computer scientists consider the array a physical structure and not an abstraction since arrays are implemented at the hardware level. But remember, there are only three basic operations available with the hardware-implemented array. As part of our Array ADT, we have provided for these operations but have also included an iterator and operations for obtaining the size of the array and for setting every element to a given value. In this case, we have provided a higher level of abstraction than that provided by the underlying hardware-implemented array.

Special Topic
The ezarrays Python Module
Page last modified on August 24, 2023, at 07:20 PM