The array is a Data structure that contains a collection of elements of the same type under a single variable name. Elements are stored under continuous memory
Features:
- It stores the elements in insertion order
- Elements are ordered on the insertion order
- Elements get using the index, Starting index=0, last element index = length-1
- Elements are not sorted
- Array contains the fixed size in length
Array Types:
There are multiple types of arrays.
One-Dimensional Array. It contains a sequence of elements in a single row. Also called as Linear Array
Two-dimensional Arrays
It is an array of array, Containing rows and columns. Matrix is a 2-dimensional array. Multi-Dimensional Arrays: It is an array of nested arrays. It can be 3x3 or nxn arrays.
Arrays Operations
Arra contains the following basic operations
- Insert an element
- Delete an element
- Search an array
- Sort An array
- Iterate each element
Array advantages
- It stores the elements in insertion order, unordered
- It uses as a basic data structure to implement other data structures, Array lists, Linked List
- Accessing elements is 0(1) and faster
Array disadvantages
- Arrays are static, so Size can not be modified
- If the array is defined with fixed size n, the number of elements stored is n-1, and memory waste exists.
- It stores data of the same type
Array time complexity for operations
Time complexity is the same for Best, average, The Worst Case
Operation | Time complexity |
---|---|
Get Elements | O(1) |
Insert an Element | O(n) |
Delete an Element | O(n) |
Search an Element | O(n) |
Sort an Elements | O(n) |