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

Introduction to Arrays

Uploaded by

sonichirag1301
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)
4 views

Introduction to Arrays

Uploaded by

sonichirag1301
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/ 3

Introduction to Arrays

An array is a data structure that stores a collection of items (values), typically of the same data type, at
contiguous memory locations. Arrays are fundamental in computer programming and are used to organize
data for efficient access and manipulation.

Key Features of Arrays

1.​ Fixed Size: Arrays have a predefined size, which means the number of elements in an array is set
during its creation.
2.​ Same Data Type: All elements in an array must be of the same data type (e.g., integers, floats,
strings).
3.​ Indexed Access: Each element in an array is identified by an index, starting from 0 in most
programming languages.
4.​ Contiguous Memory: Elements of an array are stored in contiguous memory locations, enabling
efficient access.

Why Use Arrays?

●​ Arrays allow multiple elements to be stored and accessed efficiently using indices.
●​ They are useful for operations like sorting, searching, and performing mathematical
computations.

Advantages of Arrays

●​ Fast Access: Accessing elements by index is very quick (O(1) time complexity).
●​ Efficient Memory Use: Arrays require a continuous block of memory, ensuring compact storage.

Disadvantages of Arrays

●​ Fixed Size: Once declared, the size of an array cannot be changed.


●​ Insertion and Deletion Overhead: Adding or removing elements requires shifting other
elements.

Graphical Representation
Consider an array A with 5 elements:​
A=[10,20,30,40,50]A = [10, 20, 30, 40, 50]A=[10,20,30,40,50]

Index 0 1 2 3 4

Value 10 20 30 40 50

To access the third element (30), you use the index 2

Implementation in Programming
arr = [10, 20, 30, 40, 50]
# Accessing elements
print("First element:", arr[0]) # Output: 10
print("Third element:", arr[2]) # Output: 30

# Updating an element arr[1] = 25


print("Updated array:", arr) # Output: [10, 25, 30, 40, 50]

Linear Search

Definition

Linear Search is a simple searching algorithm used to find an element in a list or array. In this method,
each element is checked sequentially until the desired element is found or the end of the list is reached.

How Linear Search Works

1.​ Start from the first element of the list.


2.​ Compare the target value with each element in the list.
3.​ If a match is found, return the index of the element.
4.​ If no match is found, return a message indicating the element is not in the list.
Algorithm

For an array/list A of size n and target value x:

1.​ Initialize i = 0.
2.​ While i < n:
○​ If A[i] == x, return i.
○​ Else, increment i by 1.
3.​ If no match is found, return -1 or "Not Found."

Operations on Array

You might also like