UNIT II: Arrays,
Searching, and Sorting
By – Payal Hake
Overview of Array
What is an Array?
An array is a linear data structure that stores elements of the same data
type in a contiguous block of memory, accessed using indices.
Key Characteristics
• Fixed size (static allocation)
• Elements are indexed from 0 to n-1
• Random access enabled (O(1) time complexity)
• Efficient storage and access for homogeneous data
Overview of Array
Declaration of Array.
• This array will store integer type element
int arr[5];
• This array will store char type element
char arr[10];
• This array will store float type element
float arr[20];
Initialization of Array
• int arr[] = { 1, 2, 3, 4, 5 };
• char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
• float arr[10] = { 1.4, 2.0, 24, 5.0, 0.0 };
Overview of Array
Why do we Need Arrays?
Easy Data Handling: Arrays store multiple values in one variable, making large
data easier to manage.
Indexed Access: Elements can be accessed and modified easily using indices.
Better Scalability: Suitable for handling large datasets without many separate
variables.
Overview of Array
Types of Arrays
• One-Dimensional Array (1D): Simple list of elements
• (e.g., int arr[5] = {1, 2, 3, 4, 5})
• Two-Dimensional Array (2D): Matrix-like structure
• (e.g., int matrix[3][3])
Overview of Array
Types of Arrays
• Multidimensional Array (nD): Higher-level data representations
• (e.g., tensor-like structures in ML)
Real-World Applications
• Storing marks of students
Array as an Abstract Data Type
What is an Abstract Data Type (ADT)?
• An ADT is a theoretical model that defines:
• The data to be stored
• The operations allowed on that data
• without specifying the internal implementation
Array as an ADT
• An array as an ADT defines:
• A collection of elements of the same type
• Fixed size (defined at creation)
Array as an Abstract Data Type
Advantages of Array as ADT
• Random Access
• Simplicity
• Memory Locality -Arrays are stored in contiguous
memory locations
• Efficient Iteration
Disadvantages of Array as ADT
• Fixed Size
• Costly Insertions/Deletions
Operations on Arrays
1. Traversal
• Visiting each element of the array from start to end
• Used for displaying, summing, or processing all elements
• 🕒 Time Complexity: O(n)
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " << arr[i] << endl;
}
return 0;
Operations on Arrays
int main() {
int arr[10] = {10, 20, 30, 40, 50}; // initial array
2. Insertion int n = 5; // current size of array
int ele, loc;
• Adding an element at a specific
index cout << "Enter element to insert: ";
cin >> ele;
• Requires shifting elements to the
right cout << "Enter location (index) to insert: ";
cin >> loc;
• 🕒 Time Complexity:
for (int i = n - 1; i >= loc; i--) {
• Best Case (end insertion): O(1)
arr[i + 1] = arr[i];
• Worst Case (front insertion): O(n) }
arr[loc] = ele;
Types of Insertion n++; // increase size
• The beginning cout << "Array after insertion: ";
Operations on Arrays
int main() {
int arr[10] = {10, 20, 30, 40, 50};
— 3. Deletion int n = 5;
• Removing an element from a specific int loc;
index
cout << "Enter index to delete: ";
• Requires shifting elements to the left to cin >> loc;
fill the gap
• 🕒 Time Complexity: O(n) for (int i = loc; i < n - 1; i++) {
arr[i] = arr[i + 1]; // shift left
}
Types of Deletion
• Beginning (Index 0): All elements n--; // reduce size
shift left
• Middle (Specific Index): Elements cout << "Array after deletion: ";
after index shift for (int i = 0; i < n; i++) {
Operations on Arrays
📝 4. Updating
• Replacing an element at a given index with a new value
• 🕒 Time Complexity: O(1)
🔍 5. Searching
• Finding the position of an element
• Types: Linear Search O(n), Binary Search O(log n) (if sorted)
Storage Representation
How Arrays are Stored in Memory
• Arrays are stored in contiguous blocks of memory
• 🠶Each element is placed at a memory address calculated using:
Address=Base Address+(i×Size of data type)
• Base Address = 1000
• Size of int = 2 bytes
1D Array Storage
• Elements are presented sequentially
• Enables constant-time access using index
Overview of Array
• Address=Base Address+(i×Size of data • Address of arr[0] = 200 + (0 × 4) = 200
type) • Address of arr[1] = 200 + (1 × 4) = 204
• Base Address = 200 • Address of arr[2] = 200 + (2 × 4) = 208
• Size of int = 4 bytes • Address of arr[3] = 200 + (3 × 4) = 212
• Address of arr[4] = 200 + (4 × 4) = 216
Multidimensional Arrays [2D,
nD] Array?
What is a Multidimensional
• A multidimensional array is an array of arrays.
• It allows storing data in table-like (2D) or cube-like (nD) structures.
2D Array
• Represents a matrix with rows and columns
• Declaration example :
data_type array_name[rows][columns];
Applications:
int arr [3] [3];
Image processing, Graph representations, Matrix operations
Accessed using: matrix[i][j]
Two-Dimensional Array (2-D)
int main() {
int rows = 2, cols = 3;
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6} Output:
}; Matrix Elements:
123
// Displaying the matrix
456
cout << "Matrix Elements:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
Multidimensional Arrays [2D, nD]
2D Array Storage
• There are two common representations:
1. Row-Major Order
All elements of the first row are stored first, followed by second, and so on.
2. Column-Major Order
All elements of the first column are stored first, followed by second, and so on.
Row-Major and Column-Major
int main() {
const int rows = 2, cols = 3;
int matrix[rows][cols] = {
{1, 2, 3},
{4, 5, 6}
}; Output:
cout << "Row-Major Order:" << endl; Row-Major Order:
for (int i = 0; i < rows; i++) { 123456
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " "; Column-Major Order:
} 142536
}
cout << "\nColumn-Major Order:" <<
endl;
for (int j = 0; j < cols; j++) {
for (int i = 0; i < rows; i++) {
Multidimensional Arrays [2D, nD]
nD Array
• Extension of 2D array into multiple dimensions
• Useful in advanced simulations, tensor computations, etc.
• Example: int tensor[3][4][2];
Applications:
Machine learning (tensors), physics simulations, 3D graphics
Sparse Matrix
Representation using 2D Arrays
What is a Sparse Matrix?
• A sparse matrix is a matrix in which most elements are zero.
Storing all elements wastes memory and computation.
Why Use Sparse Representation?
• Saves space by storing only non-zero values in large matrices.
• Significantly reduces space complexity.
• Speeds up matrix operations by ignoring zeros.
Common Use Cases
Sparse Matrix
Representation using 2D
Arrays
Sparse Matrix using 2D Array
Sparse Matrix
Representation using 2D
Arrays
Sparse Matrix using 2D Array
• 🠶One popular method is the Triplet Representation (Row,
Column, Value):
Row | Col | Value
-----|-----|-------
0 | 2 | 5
1 | 0 | 8
3 | 1 | 6
Searching Techniques
1. Sequential Search (Linear Search)
• Check each element one by one
• Works on unsorted arrays
• Time Complexity: O(n)
• Best Case: First element
• Worst Case: Last element or not found
int main() { // Linear Search
int n, target; bool found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
cout << "Enter the number of elements in cout << "Target " << target << "
the array: "; found at index " << i << endl;
cin >> n; found = true;
break;
int arr[n]; // Declare array }
}
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) { if (!found) {
cin >> arr[i]; cout << "Target " << target << " not
} found in the array." << endl;
Output: }
cout << "Enter theEntertarget element
the numbertoof elements in the array: 10
search: "; Enter 10 elements: 17 20 26return
31 44 540; 55 65 77 93
cin >> target; } search: 54
Enter the target element to
Searching Techniques
2. Binary Search
• Works only on sorted arrays
• Repeatedly divides the search space in half
• Time Complexity: O(log n)
• Requires sorting beforehand
• Binary Search is significantly faster than Linear Search for large
int main() { int binarySearch(int arr[], int n, int
int arr[] = {11, 17, 18, 45, 50, 71, 95}; target) {
int low = 0;
int high = n - 1;
int n = sizeof(arr) / sizeof(arr[0]); int mid;
int target = 50;
int result = binarySearch(arr, n, while (low <= high) {
target); mid = (low + high) / 2;
if (result != -1) if (arr[mid] == target) {
cout << "Target " << target << " return mid; // Target found
found at index " << result << endl; }
else else if (arr[mid] < target) {
cout << "Target " << target << " low = mid + 1; // Search in right
Output:
not found in the half
Target 50array."
found at<<index
endl;4
}
Searching Techniques
3. Fibonacci Search
• Based on Fibonacci numbers for split positions
• Suitable for large sorted arrays
• Time Complexity: O(log n)
• Uses less division than binary search (faster on some machines)
Searching Techniques
4. Indexed Sequential Search
• Hybrid search method that combines:
- Indexing – like Binary Search for block location.
- Sequential Search – like Linear Search within the block.
• Use a separate index table for faster access
• Efficient when working with large sorted datasets
• Indexed Search is useful when data is too large for memory and is
stored in blocks (e.g., files on disk).
Searching Techniques
Steps:
1. Read the search element from the user.
2. Divide the array into groups according to group size.
3. Create an index array that contains the starting index of the
groups.
4. If the group is present and the first element of that group is
less than or equal to the key element, go to the next group.
Otherwise, apply a linear search in the previous group.
Sorting Concepts
Why Sorting?
• Storing organizes data in a specific order (ascending/descending), making
searching and processing faster and more efficient.
Sorting Concepts
1. Stability
A sorting algorithm is stable if it keeps the original order of
elements that are equal.
📌 Example: Two students with same marks stay in the same order
after sorting.
2. Efficiency
Sorting performance is judged by:
- Time Complexity (how fast it runs)
- Space Complexity (how much memory it uses)
Evaluated using: Best, Average, and Worst cases.
Sorting Algorithms
1. Bubble Sort
• Compares adjacent elements and swaps if needed
• Repeats until the list is sorted
Time: O(n²)
Stable
Easy but inefficient for large datasets
Sorting Algorithms
1. Bubble Sort:
Sorting Algorithms
2. Insertion Sort
• Builds sorted list by inserting one element at a
time
• Best Case(Ascending order): O(N)
• Worst Case(Descending order): O(N2)
• Stable
• Efficient for small or nearly sorted arrays
• Not use extra space
Sorting Algorithms
2. Insertion Sort
Sorting Algorithms
3. Selection Sort
• Repeatedly selects the minimum element and places it at the
front Time: O(n²)
Not Stable
Simple, but not efficient
Sorting Algorithms
3. Selection Sort
Efficient Sorting Algorithms
1. Quick Sort
• Divide-and-conquer using pivot-based partitioning
• Time: O(n log n) average, O(n²) worst
• Not Stable
• Very efficient and widely used
• Not use extra space
Efficient Sorting Algorithms
1. Quick Sort
Efficient Sorting Algorithms
1. Quick Sort
1. Start with an array of elements to be sorted.
2. Choose a pivot element (can be the first, last, or middle element, or
chosen randomly).
3. Partition the array:
1. Place all elements smaller than the pivot to its left.
2. Place all elements greater than the pivot to its right.
4. Recursively apply Quick Sort to the left sub-array.
5. Recursively apply Quick Sort to the right sub-array.
6. Repeat until each sub-array has only one element or is empty.
7. Combine the sorted sub-arrays (they are naturally combined during
recursion).
8. End — the array is now sorted.
Efficient Sorting Algorithms
1. Quick Sort
PARTITION_HOARE(arr, low, high): repeat:
pivot = arr[low] j=j-1
i = low - 1 until arr[j] <= pivot
j = high + 1
// If pointers meet or cross, return j
while true: if i >= j:
// Move i right until element >= pivot return j
repeat:
i=i+1 // Swap elements at i and j
until arr[i]QUICKSORT(arr,
>= pivot low, high): swap arr[i] with arr[j]
if low
// Move j left until < high:
element <= pivot
p = PARTITION_HOARE(arr, low, high)
QUICKSORT(arr, low, p) // Sort left part
Efficient Sorting Algorithms
1. Quick Sort – Worst Case[When you have already sorted array]
Efficient Sorting Algorithms
1. Quick Sort – Best Case
Recursion and Backtracking
Efficient Sorting Algorithms
2. Merge Sort
- Divide the array, sort halves, and merge
- Stable
- Uses extra space, but reliable and consistent
Efficient Sorting Algorithms
2. Merge Sort
MERGESORT(arr, left, right):
if left < right:
mid = (left + right) // 2 // Find the middle index
MERGESORT(arr, left, mid) // Sort first half
MERGESORT(arr, mid + 1, right) // Sort second half
MERGE(arr, left, mid, right) // Merge the two halves
Efficient Sorting Algorithms
2. Merge Sort
# Copy remaining elements from left half (if
function merge(array, low, mid, high): any)
create empty temp list while left ≤ mid:
left = low temp.append(array[left])
right = mid + 1 left = left + 1
# Merge elements from both halves in # Copy remaining elements from right half (if
sorted order any)
while left ≤ mid AND right ≤ high: while right ≤ high:
if array[left] ≤ array[right]: temp.append(array[right])
temp.append(array[left]) right = right + 1
left = left + 1
else: # Copy merged elements back to the original
temp.append(array[right]) array
right = right + 1 for i from 0 to length of temp - 1:
array[low + i] = temp[i]
Efficient Sorting Algorithms
2. Merge Sort time complexity - O(n log n)
Efficient Sorting Algorithms
2. Merge Sort time complexity - O(n log n)
Exemplar / Case Study: Social
Network Adjacency Matrix
• Use Case: Representing friendship connections in social media
platforms.
• Data Structure Used: 2D Adjacency Matrix.
• Matrix Element: a[i][j] = 1 if user i is friends with user j, else 0.
• For millions of users, the matrix is very sparse.
• Sparse matrix techniques help save memory and improve
performance.
• Useful in applications like friend suggestion, community
detection, etc.
Text Books
1. Fundamentals of Data Structures in C – by Ellis Horowitz,
Sartaj Sahni, Susan Anderson-Freed Universities Press
2. Data Structures Through C - by Yashavant Kanetkar BPB
Publications
NPTEL Links
1. Sorting and Searching by IIT Kharagpur
https://www.youtube.com/watch?v=ZwFkaGTt4Xw
2. NPTEL Course - NOC: Programming, Data Structures and
Algorithms using Python
https://nptel.ac.in/courses/106106145