Introduction to Arrays
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.
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.
● 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
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
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
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.
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