0% found this document useful (0 votes)
16 views20 pages

Lecture 4

The document provides an overview of Data Structures and Algorithms (DSA), emphasizing the importance of learning DSA for enhancing problem-solving skills and its applications in various software. It focuses on arrays as a fundamental data structure, detailing their characteristics, types, and operations such as traversal, insertion, deletion, and searching. The document also explains the memory representation of arrays and the differences between fixed-sized and dynamic arrays.

Uploaded by

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

Lecture 4

The document provides an overview of Data Structures and Algorithms (DSA), emphasizing the importance of learning DSA for enhancing problem-solving skills and its applications in various software. It focuses on arrays as a fundamental data structure, detailing their characteristics, types, and operations such as traversal, insertion, deletion, and searching. The document also explains the memory representation of arrays and the differences between fixed-sized and dynamic arrays.

Uploaded by

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

Data Structures and Algorithms

Array
Lecture 4
Introduction

• DSA (Data Structures and Algorithms) is the study of organizing data


efficiently using data structures like arrays, stacks, and trees, paired
with step-by-step procedures (or algorithms) to solve problems
effectively.

• Data structures manage how data is stored and accessed, while


algorithms focus on processing this data.
Why to Learn DSA?

• Learning DSA boosts your problem-solving abilities and make you a


stronger programmer.

• DSA is foundation for almost every software like GPS, Search


Engines, AI ChatBots, Gaming Apps, Databases, Web Applications,
etc
Array

Array is a collection of items of the same variable type that are stored at
contiguous memory locations. It is one of the most popular and simple
data structures used in programming.
Basic terminologies of Array

• Array Index: In an array, elements are identified by their indexes. Array


index starts from 0.

• Array element: Elements are items stored in an array and can be


accessed by their index.

• Array Length: The length of an array is determined by the number of


elements it can contain.
Memory representation of Array

In an array, all the elements are stored in contiguous memory locations.


So, if we initialize an array, the elements will be allocated sequentially in
memory. This allows for efficient access and manipulation of elements.
Declaration & Initialization of Array
# In Python, all types of lists are created same way
arr = []

# This list will store integer type elements


arr = [1, 2, 3, 4, 5]

# This list will store character type elements (strings in


Python)
arr = ['a', 'b', 'c', 'd', 'e']

# This list will store float type elements


arr = [1.4, 2.0, 24.0, 5.0, 0.0] # All float values
Types of Array
Arrays can be classified in two ways:
On the basis of Size On the basis of Dimensions
Types of Array (On the basis of Size)
Fixed Sized Arrays:

# Create a fixed-size list of length 5,


# initialized with zeros
arr = [0] * 5
# Output the fixed-size list
print(arr)

Result:
[0, 0, 0, 0, 0]
Types of Array (On the basis of Size)
Dynamic Sized Arrays:

# Dynamic Array
arr = []
Types of Array (on the basis of Dimensions)
One-dimensional Array(1-D Array):
Types of Array (on the basis of Dimensions)
Multi-dimensional Array(2-D Array):
Operations on Array
1. Array Traversal:
The process of accessing and processing each element of an array
sequentially.
For example,
arr = [10, 20, 30, 40, 50]
Here:
•The first element (10) is at index 0.
•The second element (20) is at index 1.
•The last element (50) is at index 4.
Operations on Array
1. Array Traversal:
The process of accessing and processing each element of an array
sequentially.
For example,
arr = [10, 20, 30, 40, 50]
Here:
•The first element (10) is at index 0.
•The second element (20) is at index 1.
•The last element (50) is at index 4.
Operations on Array
2. Insertion in Array:
The process of adding a new element at a specific position while
maintaining the order of the existing elements.
Arrays are stored in contiguous memory locations, meaning elements are arranged
in a sequential block. When inserting a new element, the following happens:

• Identify the Position: Determine where the new element should be inserted.
• Shift Elements: Move the existing elements one position forward to create space
for the new element.
• Insert the New Element: Place the new value in the correct position.
• Update the Size (if applicable): If the array is dynamic, its size is increased.
Operations on Array
3. Deletion in Array:
The process of removing an element from a specific position while
maintaining the order of the remaining elements.
Since arrays have contiguous memory allocation, deleting an element does not
reduce the allocated memory size. Instead, it involves:

• Identify the Position: Find the index of the element to be deleted.


• Shift Elements: Move the elements after the deleted element one position to the
left.
• Update the Size (if applicable): If using a dynamic array, the size might be
reduced.
Operations on Array
4. Searching in Array:
The process of finding a specific element in a given list of elements.
There are two main types of searching techniques in an array:

1. Linear Search (Sequential Search):


• This is the simplest search algorithm.
• It traverses the array one element at a time and compares each element with the target value.
• If a match is found, it returns the index of the element.
• If the element is not found, the search continues until the end of the array.

2. Binary Search (Efficient Search for Sorted Arrays)


• Works only on sorted arrays (in increasing or decreasing order).
• Uses a divide and conquer approach.
• It repeatedly divides the search space in half until the target element is found.

You might also like