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

03 Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

03 Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures

Lecture #03: Arrays Data Structures

IT & Computer Department


Spring Term 2024 – Semester-7

Presented by: Munir Khalifa.


Today’s Outline

❖ What are Arrays?


❖ Array Representation
❖ Applications of Array
❖ Array features
❖ Arrays Time Complexity
❖ Advantages & disadvantages of Arrays
Data Structures
What are Arrays?
Imagine that we have 100 scores. We need to read them,
process them, and print them. We must also keep these 100
scores in memory for the duration of the program. We can
define a hundred variables, each with a different name.
What are Arrays?
But having 100 different names creates other problems. We
need 100 references to read them, 100 references to
process them and 100 references to write them. Figure
shows a diagram that illustrates this problem.
What are Arrays?
• An array is a collection of elements stored at contiguous
memory locations, accessible by an index or a key.
Arrays have fixed sizes in most programming languages.
• A list of values with the same data type that are stored
using a single group name (array name).
• General array declaration statement:
data-type array-name [number-of-items]
• The number of items must be specified before declaring
the array.
const int SIZE = 100;
float arr[SIZE];
Array Representation
Applications of Array
Arrays are a fundamental data structure in computer science. They are used
in a wide variety of applications, including:
• Storing data for processing
• Implementing data structures such as stacks and queues
• Representing data in tables and matrices
• Creating dynamic data structures such as linked lists and trees
Array features
• Homogeneous: All elements in an array share a common data type,
allowing them to have a uniform size. This is different from Python lists,
which are allowed to contain elements of mixed types.

• Contiguous: Array elements are adjacent to each other in the computer


memory, occupying a contiguous block of memory space. No other data is
allowed between the beginning and the end of an array. Such a layout
corresponds to the way your computer memory is logically organized.

• Fixed-Size: Because other data may follow immediately after the block of
memory occupied by your array, expanding the block would overwrite that
data, potentially causing severe errors or malfunctions. Therefore, when
you allocate memory for an array, you must know its size upfront without
the possibility of changing it later.
Array Cont…
Types of Array: There are two main types of arrays:

• One-dimensional arrays: These arrays store a single row of


elements.
• Multidimensional arrays: These arrays store multiple rows of
elements.

Array Operations: Common operations performed on arrays include:

• 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.
Representation of Arrays in Data Structures

Syntax - The syntax of creating an array is:

Parameters
• Type code − The type code character used to create the array.

• initializer − array initialized from the optional value, which must be a list,
a bytes-like object, or inerrable over elements of the appropriate type.
Representation of Arrays in Data Structures

Example
Representation of Arrays in Data Structures

Accessing Array Element


Basic Operations on Arrays in Data Structures

Traversal
Basic Operations on Arrays in Data Structures

Insertion
Basic Operations on Arrays in Data Structures

Insertion
Basic Operations on Arrays in Data Structures

Deletion Operation
Basic Operations on Arrays in Data Structures

Deletion Operation
Basic Operations on Arrays in Data Structures

Search Operation
Basic Operations on Arrays in Data Structures

Update Operation
Complexity Analysis of Operations on Arrays
Advantages of Arrays in Data Structures

• Efficient access: Arrays offer fast and efficient access to elements


because each element can be accessed directly through its index.
This makes array traversal quick and straightforward.
• Versatility: Arrays can be used to store any type of data like
integers, characters, and strings. They can also be used to store
user-defined data types, such as structures and classes.
• Flexibility: Arrays are used to implement other data structures like
stacks, queues, trees, graphs, etc.
• Easy to remember: Arrays represent multiple data items of the
same type using a single name. Therefore it’s easier to remember
an array name than remembering the names of several variables.
disadvantages of Arrays in Data Structures

• Fixed-size: The size of an array is fixed at the time of its creation, which means that
once the array is created, its size cannot be changed. This can be a limitation in
situations where the size of the data is not known in advance.
• Memory wastage: There will be a wastage of memory if we store less number of
elements than the declared size because there is static memory allocation in arrays.
• Inefficient insertion and deletion: Arrays store data in contiguous memory locations,
which makes deletion and insertion very difficult to implement. All the elements after
insertion or deletion must be shifted to accommodate the new element or fill in the
gap. This can be a time-consuming process, especially for large arrays.
• Homogeneous data: Arrays can only store elements of the same data type, which
can be a limitation in situations where the user needs to store data of different types.
• No built-in support for dynamic resizing: While some programming languages provide
built-in support for dynamic resizing of arrays, many do not. In those cases, the
developer may have to implement their resizing logic, which can be complex and
error-prone.

You might also like