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

02. Array

The document provides an overview of data structures, focusing on arrays as a linear data structure where elements are stored sequentially in contiguous memory locations. It discusses the characteristics of arrays, types of arrays based on dimensions, and the differences between static and dynamic arrays, as well as memory representation in different programming languages. Additionally, it covers operations that can be performed on arrays, including insertion, deletion, and sorting methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

02. Array

The document provides an overview of data structures, focusing on arrays as a linear data structure where elements are stored sequentially in contiguous memory locations. It discusses the characteristics of arrays, types of arrays based on dimensions, and the differences between static and dynamic arrays, as well as memory representation in different programming languages. Additionally, it covers operations that can be performed on arrays, including insertion, deletion, and sorting methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Data Structures

Dr. Narjes Nikzad Khasmakhi


[email protected]
Array

2
Questions

❖ What are linear data structures?


❖ What is the difference between linear data structure and non-
Linear data structure?
❖ What is Array data structure?
❖ What are different types of Arrays?
❖ What is data storage or memory representation?
❖ How is memory allocated to arrays in different programming
3
languages?
Questions

❖ What operations can be performed on arrays?

4
Types of Data Structure

5
https://simplerize.com/data-structures/data-structure-introduction
Linear Data Structure vs Non-Linear Data Structure

❖ Linear data structures,


like arrays and linked
lists, store data
sequentially, while
non-linear data
structures, like trees
and graphs, organize
data hierarchically or
in interconnected
networks. 6
Types of Data Structure

7
https://simplerize.com/data-structures/data-structure-introduction
Linear Data Structure

A Data structure is said to be linear if its elements from a sequence or in


other words form a linear list.

8
Types of Linear Data Structures

❖ Have a linear relationship between the elements represented by means


of sequential memory locations [Arrays].
❖ Have the linear relationship between the elements represented by
means of pointer or links [ Linked List].

9
Types of Data Structure

10
https://simplerize.com/data-structures/data-structure-introduction
Array

❖ Array is a linear data structure where all elements are arranged


sequentially. It is a collection of elements of same data type stored at
contiguous memory locations.

11
Characteristics of Arrays in Data Structures

❖ Fixed Size: Once an array is declared, its size cannot be changed. You
need to know the number of elements you will store in the array
beforehand.
❖ Homogeneous Elements: All elements in an array must be of the same
data type, such as all integers, all floats, or all characters.

12
Characteristics of Arrays in Data Structures

❖ Indexed by Integers: Each element in an array is assigned a unique


integer called an index, which identifies its position within the array.
Indexing usually starts at 0.
❖ Efficient Access: Because of the way arrays are stored in memory
(contiguously), accessing any element by its index is very efficient. This
direct access using the index is often referred to as "random access."

13
Types of Arrays based on Dimensions

❏ One-Dimensional Arrays

❏ Two-Dimensional Array

❏ Multi-Dimensional Array

14
Types of Arrays based on Dimensions-One-
Dimensional Array

❖ These are the simplest form of arrays, consisting of a single line of


elements. Each element in a 1D array is accessed by a single index
representing its position within the array.

15
Types of Arrays based on Dimensions-One-
Dimensional Array

❖ Example: A small shop could use a 1D array to keep track of the quantity
of each product in stock. Each index in the array represents a different
product, and the value at that index represents the current stock level.

16
Types of Arrays based on Dimensions - Two-
Dimensional Array

❖A two-dimensional array in data


structure, often thought of as a matrix,
consists of rows and columns. It is
used to represent data in a grid format
and is accessed by two indices: one
for the row and another for the column.

17
Types of Arrays based on Dimensions - Multi-
Dimensional Array

❖ These are the arrays with more


than two dimensions.
❖ The multi-dimensional arrays in
data structures can be used in
scenarios requiring complex data
representation.

18
Types of Arrays based on Dimensions: Summary

19
Types of Arrays based on Dimensions

20
Basics of Array in Data Structures

❖ Static Arrays

❖ Dynamic Arrays

21
Basics of Array in Data Structures

Static Arrays

❖ A static array has a fixed size, which means the number of elements it
can hold is defined when the array is declared and cannot be

changed during runtime.


❖ The memory for a static array is allocated at compile time, and the
allocated memory is a contiguous block that remains allocated for the

entire lifetime of the array. 22


Basics of Array in Data Structures

Dynamic Arrays

❖ Dynamic arrays can change size during runtime, allowing more


flexibility than static arrays.

❖ They are particularly useful when the number of elements is not


known in advance. Dynamic arrays are implemented using pointers,
and memory for them is allocated at runtime from the heap, which

allows them to expand or contract as needed. 23


Data Structure vs. Storage Structure

❖ Data Structure: The logical or mathematical model of a particular

organization of data

❖ Storage Structure: Representation of a particular data structure in the

memory of a computer

❖ There are many possible storage structure to a particular data structure.


❖ It is possible for two DS to represented by the same storage structure.
24
Types of Storage Data

https://blog.bytebytego.com/p/storage-systems-overview

25
Storage Structure Or Memory Representation

❖ Memory representation is about how data structures like arrays are


stored in a computer’s memory.

26
Memory Representation in Different Languages

❖ Different programming languages handle memory representation


slightly differently, especially when it comes to arrays:

➢ C/C++: Arrays in C are straightforward and stored in contiguous


memory. If you need a dynamic array, you have to manage
memory manually using functions like malloc and realloc.
➢ Java: In Java, arrays are also stored contiguously, but Java also
provides higher-level data structures like ArrayList that
automatically handle resizing for you.
➢ Python: Python lists are dynamic, meaning they automatically
grow when you append new elements. 28
In C++, memory allocation for arrays

● The amount of memory that an array uses depends on the data type used
to create the array.

29
In Java, memory allocation for arrays

● The amount of memory that an array uses depends on the data type used
to create the array.

30
Why not mixed types

31
Why not mixed types

32
Why not mixed types

33
Why not mixed types

34
Why not mixed types

35
Why not mixed types

36
Why not mixed types

37
Why Java script, Python, .. does not have any problem?

38
Why Java script, Python, .. does not have any problem?

39
Why Java script, Python, .. does not have any problem?

40
Why Java script, Python, .. does not have any problem?

41
JavaScript for mixed type

In JavaScript, the memory size of a


string is generally calculated based
on the number of characters in the
string, where each character
requires 2 bytes (16 bits) of
memory.

42
JavaScript for mixed type

43
JavaScript for mixed type

44
JavaScript for mixed type

45
Operations on Array

47
Insertion in Arrays

Types of Insertion

● At the Beginning: Insert an element at the start of the array.


● At the End: Add an element at the end of the array.
● At a Specific Position: Insert an element at a given index in the
array.

48
Deletion in Array

Types of Deletion
● At the Beginning: Remove
the first element of the array.
● At the End: Remove the last
element of the array.
● At a Specific Position:
Remove an element from a
given index in the array.

49
Sorting an array

❖ Counting Sort
❖ Insertion Sort
❖ Bubble Sort
❖ Selection Sort
❖ Merge Sort
❖ Quick Sort

50
THANKS!

59
60

You might also like