Detailed_Array_Summary
Detailed_Array_Summary
1. Introduction to Arrays
An array is a collection of elements stored in contiguous memory locations.
It allows multiple values of the same data type to be stored and accessed using a single variable name.
Characteristics:
Types of Arrays:
- 2D Array: A table-like structure with rows and columns (e.g., int arr[3][4]).
- Multi-dimensional Arrays: Arrays with more than two dimensions (e.g., int arr[3][4][5]).
3. Array Operations
Insertion: Adding an element at a specific position. Time complexity: O(n).
Traversal: Accessing and printing all elements sequentially. Time complexity: O(n).
Searching:
- Linear Search: Sequentially checks each element until the target is found. Time complexity: O(n).
- Binary Search: Efficient for sorted arrays; repeatedly divides the array in half. Time complexity: O(log n).
Sorting:
- Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order. Time complexity: O(n²).
- Selection Sort: Selects the smallest element and swaps it with the first unsorted element. Time complexity:
O(n²).
- Merge Sort: Divides the array into halves, sorts each half, and merges them. Time complexity: O(n log n).
4. Applications of Arrays
1. Storing data in programs.
5. Sample Programs
Example 1: Traversing an Array:
#include <stdio.h>
int main() {
return 0;
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
int main() {
if (index != -1)
else
return 0;
#include <stdio.h>
int main() {
if (index != -1)
else
return 0;
Key Takeaways
Arrays are simple yet powerful for storing and manipulating data.
They offer fast random access but have limitations, such as fixed size and costly insertions/deletions.
Mastering array operations is essential for understanding advanced data structures like linked lists, trees, and
graphs.