Arrays in DataStructures
Overview, Operations, Searching &
Sorting Techniques
2.
Introduction to Arrays
•Array is a linear data structure that stores
elements of the same type in contiguous
memory locations.
• Each element is identified by an index.
• Fixed size, and elements can be accessed using
the index (0-based).
• Efficient for read operations and simple
implementation.
3.
Array Declaration &Types
• Declaration: int arr[5]; // Declares an array of
5 integers
• Types of Arrays:
• • One-Dimensional Array
• • Two-Dimensional Array
• • Multi-Dimensional Array
4.
Array Operations
• Traversal– Visit each element sequentially.
• Insertion – Add an element at a given index.
• Deletion – Remove an element from an index.
• Searching – Find position of an element.
• Updating – Change value of an element.
5.
Searching Techniques
• 1.Linear Search:
• - Traverse array from start to end.
• - Compare each element with target.
• - Time Complexity: O(n)
• Algorithm:
• for i from 0 to n-1:
• if arr[i] == target: return i
• return -1
6.
Sorting Techniques
• 1.Bubble Sort – Repeatedly swap adjacent
elements if out of order. Time: O(n^2)
• Algorithm:
• for i in range(n):
• for j in range(0, n-i-1):
• if arr[j] > arr[j+1]: swap(arr[j], arr[j+1])
• 2. Insertion Sort – Insert element in the
correct position in sorted part. Time: O(n^2)
7.
Advantages & Disadvantagesof
Arrays
• Advantages:
• • Easy to implement.
• • Fast access using index.
• • Efficient memory use for fixed-size data.
• Disadvantages:
• • Fixed size – cannot expand.
• • Insertion/Deletion requires shifting – costly.
• • Wastage of memory if not fully used.