Array is a simple data-structure where we store same type of data. Array can not store multiple types of data. There are different types of Array. And the way in which data is stored within the Array can have infinite combinations. Few examples:- Suppose we will store in it non duplicate values , or we can store it duplicate values or sorted values. And in a similar fashion we can perform different actions on this data sets like insert, update, delete and search. So I will discuss here different strategies to store the data.
Searching Element :- The time complexity will be O(1) if index is given. And if we want to display all the array , then time complexity is O(n) as here we have to loop the whole array.
Deleting Element:- The time complexity is O(n) as here we have to shift the location of other arrays one point up. Now what if we are deleting the last position of the Array. Then it should be O(1), which is more like popping out the last element.
Inserting Element:- Also here the time complexity is O(n) as we have to loop through out the length of the array. And if still we insert at the very first position still we have shift whole array to one position.
Now in the below section, I am attaching the code in java. Here I have written it within one main method, which is really a bad approach. But for understanding purpose you can follow the below code.
public class ArrayApp {
public static void main(String[] args) {
int[] arr; //reference of Array object
arr = new int[20]; // initialization of new array objects
int totalNumberOfElemets = 0; //Currently the array is empty
int loopcounter; //will ount the loop it will mone along with the array
int storedValue; //thid values will be stored in the array and can be accessed via loopcounter or index number
//now insert the value inside the array
arr[0] = 3;
arr[1] = 3;
arr[2] = 4;
arr[3] = 89;
arr[4] = 100;
//now the number of elements inside the array is 3
totalNumberOfElemets = 4;
// Display all the items
for (loopcounter = 0; loopcounter <= totalNumberOfElemets; loopcounter++) {
System.out.println(arr[loopcounter]);}
//Now asasuming the array does not contain the duplicate elements search will work as follows
//here the key will be search elements
int iwanttofind = 40;
for (loopcounter = 0; loopcounter <= totalNumberOfElemets; loopcounter++) {
if (arr[loopcounter] == iwanttofind) {
System.out.println("The position of the element in the array is " + loopcounter);
break;
}
}
System.out.println("loopcounter"+loopcounter);
if (arr[loopcounter] != iwanttofind) {
System.out.println("Key not present in this array");
} else {
System.out.println("Found the key last");
}
//Now delete the items from the Array
//Now for delete , we have to search the item first , then delete it and then move all the digit one plae up after the index number at which the
/// deleted key present
/// So here we have to loop through two different for loops, one for searching and then to move up the elements
//So the cost is O(A+B) where A is the number of firs loop and B is the length of seconf group
int wanttodelte = 4;
//searching the position of the element
for (loopcounter = 0 ; loopcounter<= totalNumberOfElemets ; loopcounter ++)
{
System.out.println(wanttodelte + "is 4");
if (arr[loopcounter] == wanttodelte)
{
System.out.println("The position is " + loopcounter);
break;
} }
System.out.println("loopcounter lasr "+loopcounter);
//Now loopcounter contains the value of the position where the element which we want to delete is present
//now start again another loop which will move up the position
for (int k = 0 ; k<= totalNumberOfElemets -1; k++)
{
arr[loopcounter] = arr[loopcounter+1];
loopcounter++;
System.out.println(loopcounter + "loopcouner is");
System.out.println(totalNumberOfElemets + "is total element number" );
}
///now search all the array
totalNumberOfElemets--;
for (loopcounter = 0; loopcounter <= totalNumberOfElemets; loopcounter++) {
System.out.println(arr[loopcounter]);
}}}
Bookmarked!, I love your website!
Thanks Gayla!!!
Bookmarked!, I enjoy it!
This design is spectacular! You most certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!