0% found this document useful (0 votes)
1 views

7. Data Structures and Algorithms - Arrays

The document explains the concept of arrays as a fixed-size container for storing elements of the same type, detailing key terms such as element and index. It covers basic operations on arrays including traversal, insertion, deletion, search, and update, with examples in Python. Additionally, it highlights the importance of typecodes for defining the type of values an array can hold.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

7. Data Structures and Algorithms - Arrays

The document explains the concept of arrays as a fixed-size container for storing elements of the same type, detailing key terms such as element and index. It covers basic operations on arrays including traversal, insertion, deletion, search, and update, with examples in Python. Additionally, it highlights the importance of typecodes for defining the type of values an array can hold.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Structures and

Algorithms - Arrays
Array is a container which can hold a fix number of items and these items
should be of the same type. Most of the data structures make use of arrays
to implement their algorithms. Following are the important terms to
understand the concept of Array.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index,
which is used to identify the element.
Array Representation
Arrays can be declared in various ways in different languages. For
illustration, let's take C array declaration.
Arrays can be declared in various ways in different languages. For
illustration, let's take C array declaration.

As per the above illustration, following are the important points to be


considered.
 Index starts with 0.
 Array length is 10 which means it can store 10 elements.
 Each element can be accessed via its index. For example, we can
fetch an element at index 6 as 9.
Basic Operations
Following are the basic operations supported by an array.
 Traverse − print all the array elements one by one.
 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the value.
 Update − Updates an element at the given index.
Array is created in Python by importing array module to the python program. Then the
array is declared as shown eblow.

Typecode are the codes that are used to define the type of value the array will hold.
Some common typecodes used are:
Before looking at various array operations lets create and print an array using
python.
The below code creates an array named array1.

When we compile and execute the above program, it produces the following result −
Output
Accessing Array Element
We can access each element of an array using the index of the element.
The below code shows how

When we compile and execute the above program, it produces the


following result − which shows the element is inserted at index position 1.
Output
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of
array.
Here, we add a data element at the middle of the array using the python in-built insert()
method.

When we compile and execute the above program, it produces the following result which shows the
element is inserted at index position 1.
Output
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all
elements of an array.
Here, we remove a data element at the middle of the array using the python in-
built remove() method.

When we compile and execute the above program, it produces the


following result which shows the element is removed form the array.
Output
Search Operation
You can perform a search for an array element based on its value or its
index.
Here, we search a data element using the python in-built index()
method.

When we compile and execute the above program, it produces the following
result which shows the index of the element. If the value is not present in the
array then the program returns an error.
Output

Update Operation

Update operation refers to updating an existing element from the array at a given
index.
Here, we simply reassign a new value to the desired index we want to update.
When we compile and execute the above program, it produces the
following result which shows the new value at the index position 2.
Output

You might also like