Hi , I'm Sannidhi .
I am currently working on 'ARRAY DATA STURUCTURE DOCUMENTATION' problem statement .
you can reach me on:
Mail: [email protected]
- This project explains Array Data Structure ,its working .
- fuctions used in array .
- Time complexity and Space complexity of each operation performed on the Array .
An array is a linear data structure that stores a collection of elements of the same data type.
It is one of the simplest data structures and is used to represent a fixed-size sequential collection of elements.
Each element in an array is identified by an index or a key, which is an integer value.
- Arrays have a number of useful properties:
- including constant time access to individual elements
- efficient memory usage, and fast traversal of elements in sequence.
- They are commonly used in algorithms and data processing applications
The following are the common operations that can be performed on an array:
- Insertion: Adding an element at a specified index
- Deletion: Removing an element from a specified index
- Traversal: Visiting each element in the array in sequence
- Search: Finding the index of a specified element in the array
- Sorting: Rearranging the elements of the array in a specified order
In Java, all arrays are dynamically allocated.Arrays are stored in contiguous memory [consecutive memory locations].Since arrays are objects in Java, we can find their length using the object property length.A Java array variable can also be declared like other variables with [] after the data type. The variables in the array are ordered, and each has an index beginning with 0.Java array can also be used as a static field, a local variable, or a method parameter. The size of an array must be specified by int or short value and not long.Every array type implements the interfaces Cloneable and java.io.Serializable.
syntax : dataType[] arrayName;
examples : double[] data = new double[10];
declare and initialize and array : int[] age = {12, 4, 5, 2, 5};
Avereage: O(1)
worst:O(1)
O(1)
This function inserts a value at the specified index in the array. If the index is already occupied, the function shifts all the elements to the right of the index to make room for the new value.
static void insertElement(int arr[], int n, int x,int pos)
{
// shift elements to the right which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
Avereage: O(n)
worst:O(n)
O(1)
In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element
static int findElement(int arr[], int n, int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
Avereage: O(n)
worst:O(n)
O(1)
In the delete operation, the element to be deleted is searched using the linear search, and then the delete operation is performed followed by shifting the elements.
static int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = findElement(arr, n, key);
if (pos == -1) {
System.out.println("Element not found");
return n;
}
Avereage: O(n)
worst:O(n)
O(n)
void reaverse(int arr[],int n){
for (int i = 0; i < n; i++){
System.out.println(arr[i]);
}
}
Avereage: O(n)
worst:O(n)
O(1)
Sorting the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected element with rest of the elements.
void sort(){
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
Avereage: O(n log n)
worst:O(n log n)
O(1)
Arrays are a simple and efficient data structure used to store a collection of elements of the same data type. They have a number of useful properties, including constant time access to individual elements, efficient memory usage, and fast traversal of elements in sequence. However, their performance may degrade for insertion and deletion operations