- Array is an Data Structure ,which store the collection of homogeneous data types values,means data types of all data elements must be same.
Example :
Array Indexing
- Array is consist two important things : first is the Data element and second is the index.
- There are three types of array index representation :
1- Zero-Based Indexing
2- One- Based Indexing
3- Random indexing - Zero-Based Indexing : indexing will start from 0, then first element of array will be at index 0. means base element will be at index-0.
- One- Based Indexing : indexing will start from 1, then first element of array will be at index 1. means base element will be at index-1.
- Random indexing : indexing will start from any any number , means first element of array can be at index number.
Generally almost Programming Language support Zero-Based Indexing in their implementation.Like C ,C++, JAVA , C# and many more.
index is use to access the data element by their position. means if array name is ABC ,then ABC[2] means Data element of index 2 of array ABC ,and it is ABC[2]=66.
Array Addressing
- Every Data element have unique memory address.if we have the address of the base value or address of the 0-index value then we can calculate address of another data element very easily.Suppose integer size is 2 bytes.
Arr[0] is the base element of array. base element is actually is an pointer and it is unchangeable.
Here Address of the base element is 1000, by the using below formula we can calculate the another data element address.
Here B =1000 ,S=2 (Size of Integer) , we want to calculate the address of index-3(Arr[3]). then use above formula :
N =[1000 + 2*3] =[1006], Now Address of Arr[3] is 1006.
- Each element of array can be search randomly by using base value of array.because we can find the length of array and we can directly access randomly by their index value,but range of index will {0 to (length of array)-1}
- Every element of array is stored in memory with Contiguous memory allocation fashion.
- Size of the array is the summation of all data element size.
Example :
Suppose integer size is 2 byte(for 32 System). here length of Arr is 5 then Size of Array will be Size(Arr) = 5*2 =10 byte.
- Every Data element has always same size.
- Accessing Time of an element is always O(1) ,because Accessing requires only two simple operation addition and multiplication.
Array implementation in C and C++
In C and C++ Language array implementation is following :
int Arr[5]; // This Array Declaration
int Arr[] ={40,80,30,10,78}; // This is Array Initialization.
Array Accessing program in C is :
#include <stdio.h>
#define SIZE 5
int main()
{
int Arr[SIZE] ={40,80,30,10,78}; // This is Array Initialization.
// Access the every element by using For Loop and Print it.
for(int i=0;i<SIZE;i++)
{
printf("Element [%d] is %d\n",i,Arr[i]);
}
return 0;
}
Array Operations
- Insertion − Addition of an element at the given index.
- Traversing − visiting at all elements in the array one by one.
- Deletion − Deletion of an element at the given index.
- Search − Searching of an element in the array using the given index or the value.
- Update − Over write an element at the given index.
Types of Array
There are following types of arrays :
- One Dimensional Array (1-D Array)
- two Dimensional Array (2-D Array)
- Multi Dimensional Array (more than 2 dimensional)
- Dynamic Array.
Advantage of Array
- Array implementation is simple and easy to use.
- Accessing time of an element is very fast(Constant time).
- For fixed Size memory requirement ,Array is the best choice.
- Cache locality of Array is very good.
Disadvantage of Array
- Fixed Array Size.
- Pre-Allocation of memory for array.
- Position based insertion requires more time complexity.
- Wastage of Unused memory block for array means if we allocate memory for 80 bytes and we use only 60 bytes then remaining memory is blocked for array but not used.