Complete Core CS notes for B.Tech / B.E. Covers all important topics with formulas, examples and PYQ solutions.
c
int arr[10]; // Integer array of size 10
float marks[5]; // Float array of size 5
char name[20]; // Character array
`
Types of Arrays
1. 1D Array — Single row: int a[5] = {1,2,3,4,5}
2. 2D Array — Matrix: int matrix[3][3]
3. Multi-dimensional — 3D and beyond
Memory Representation
• Elements stored in consecutive memory locations
• Address of element: Base_Address + (index × size_of_datatype)
• Example: If base = 1000, int size = 4 bytes
- a[0] = 1000, a[1] = 1004, a[2] = 1008
Operations & Time Complexity
| Operation | Time Complexity |
|-----------|----------------|
| Access | O(1) |
| Search | O(n) linear |
| Insertion | O(n) |
| Deletion | O(n) |
Important Algorithms on Arrays
Linear Search
`c
int linearSearch(int arr[], int n, int key) {
for(int i=0; i<n; i++)
if(arr[i] == key) return i;
return -1;
}
`
Binary Search (sorted array only)
`c
int binarySearch(int arr[], int n, int key) {
int low=0, high=n-1;
while(low <= high) {
int mid = (low+high)/2;
if(arr[mid]==key) return mid;
else if(arr[mid]<key) low=mid+1;
else high=mid-1;
}
return -1;
}
``