UNIT 1 - Array Based Implementation
UNIT 1 - Array Based Implementation
STRUCTURES
ARRAY BASED
IMPLEMENTATION
Introduction
• An Array is a collection of memory locations.
• The memory locations cannot be split from
each other, so if there is some empty space, it
just goes waste.
• Arrays cannot be interchanged.
• A array is a collection of variables of same
type.
• Some points to be noted about an array:
1. It is a continuous chunk of memory with equally sized
blocks.
2. The subscript always begins with ‘0’ and goes until ‘n-
1’ which is also continuous.
3.Any memory location can be directly accessed using its
subscript.
4. Static arrays have issues, such as memory wastage
and memory leak, and also have constraints over the
size. These are overcome by using dynamic arrays.
5. Since it is a continuous chunk of memory, it has
internal issues such as fragmentation and memory leaks.
Why do we study Arrays?
• The array structure looks very similar to Python's list structure.
• But there are two major differences between the array and the
list.
1. array has a limited number of operations whereas Lists provides a
large number of operations for working with the contents of the list.
2. lists can grow and shrink during execution as elements are added or
removed while the size of an array cannot be changed after it has
been created.
• The array is best suited for problems requiring a sequence in
which the maximum number of elements are known up front,
whereas the list is the better choicewhen the size of the
sequence needs to change after it has been created.
Array Abstract Data Type
• Definition: 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, value ): 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.
• clearing( value ): Clears the array by setting every element to
value.
• iterator (): Creates and returns an iterator that can be used to
traverse the elements of the array.
Basic operations of Arrays
Creates a one-dimensional array consisting of size elements with each
Array( size ):
element initially set to None. size must be greater than zero.
Returns the value stored in the array at element position index. The
getitem ( index ) index argument must be within the valid range. Accessed using the
subscript operator.
Creates and returns an iterator that can be used to traverse the elements
iterator ()
of the array.
The ctypes Module
• Many of the data types and classes available in Python are actually
implemented using appropriate types from the C language.
• While Python does not provide the array structure as part of the language
itself, it now includes the ctypes module as part of the Python Standard
Library.
• The ctypes module provides the capability to create hardware-supported
arrays just like the ones used to implement Python's string, list, tuple, and
dictionary collection types.
• But the ctypes module is not meant for everyday use in Python programs.
• Thus, the technique provided by the module for creating an array should
not typically be used directly within a Python program.
• But we can use it within our Array class to provide the functionality defined
by the Array ADT.
Concept of Arrays
Syntax of Arrays
Creating an Array in Python
• In Python, arrays are different from lists; lists can have array
items of data types, whereas arrays can only have items of the
same data type.
• Python has a separate module for handling arrays called array,
which you need to import before you start working on them.
Ways to create Arrays
• You can declare an array in Python while initializing it using
the following syntax.
• Identifier: specify a name like
usually, you do for variables
• Module: Python has a special
module for creating arrays, called
"array" – you must import it before
using it
• Method: the array module has a
method for initializing the array. It
takes two arguments, typecode,
and elements.
• Type Code: specify the data type
using the typecodes available.
• Elements: specify the array
elements within the square
brackets, for example
[130,450,103]
Accessing an Array value
Array Operations - Insertion
Array Operations- Deletion
Array Operations - Searching
Array Operations- Update
Array Operations - Traverse