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

Introduction to Arrays

Uploaded by

aryasagar0412
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Arrays

Uploaded by

aryasagar0412
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to Arrays

Introduction to Arrays

• Introduction to Arrays
• Declaration of Arrays
• Types of Arrays
• Operations on Arrays
• Searching Techniques in Arrays
• Binary Search
Introduction to Arrays

• Definition and Purpose: Arrays are data


structures that store multiple values of the same
type in a contiguous block of memory. Used to
organize data so that it can be easily accessed
and manipulated.
• Array Elements and Indexing: Each element in an
array is identified by an index, which is a
numerical representation of its position. Indexing
starts at 0, meaning the first element is at index
0, the second at index 1, and so on.
• Fixed Size: Arrays have a fixed size, determined at
the time of declaration. This makes them
different from other data structures like linked
lists, which can grow and shrink dynamically.
Declaration of Arrays

Syntax in Different Languages: C: `int


arrayName[arraySize];`

Java: `int[] arrayName = new int[arraySize];`


• Python: `arrayName = [None] * arraySize`
• Initialization During Declaration: Arrays can be
initialized at the time of declaration.
• Example: `int arrayName[] = {1, 2, 3, 4, 5};`
• Dynamic Array Allocation: Some languages, like
C++, allow dynamic allocation using pointers.
• Example: `int* arrayName = new int[arraySize];`
Types of Arrays

• One-Dimensional Arrays: Also known as single-


dimensional arrays. Consist of a single row of
elements. Example: `int arr[5] = {1, 2, 3, 4, 5};`
• Two-Dimensional Arrays: Arrays of arrays,
forming a matrix-like structure. Represented in
rows and columns. Example: `int arr[2][3] = {{1, 2,
3}, {4, 5, 6}};`
• Multi-Dimensional Arrays: Arrays with more
than two dimensions. Used for complex data
structures. Example: `int arr[2][2][2] = {{{1, 2}, {3,
4}}, {{5, 6}, {7, 8}}};`
Operations on Arrays

• Traversal: Accessing each element of the array


for processing. Example: Printing all elements in
an array.
• Insertion: Adding a new element at a specific
position in the array. Can be costly as it may
require shifting elements.
• Deletion: Removing an element from a specific
position in the array. Requires shifting elements
to fill the gap.
• Searching: Finding the index of a specific element
in the array. Common techniques include Linear
Search and Binary Search.
• Sorting: Arranging elements of the array in a
specific order (ascending or descending).
Common techniques include Bubble Sort,
Searching Techniques in Arrays

Linear Search: Scans each element of the array


sequentially until the target element is found or the
end is reached.

Pseudo code:

```

function linearSearch(arr, target):

for i from 0 to length(arr) - 1:

if arr[i] == target:

return i

return -1
Binary Search

• Definition: Binary Search is an efficient algorithm


for finding an item from a sorted list of items by
repeatedly dividing the search interval in half.
• How It Works: The search interval is initially the
entire array. Compare the target value to the
middle element of the array. If the target value is
equal to the middle element, the search is
complete. If the target value is less than the
middle element, repeat the search on the left
half. If the target value is greater than the middle
element, repeat the search on the right half.
• Pseudo Code: ```
• function binarySearch(arr, target):
• low = 0
• high = length(arr) - 1

You might also like