Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




What are the Operations that can be performed on arrays?

Following operations can be performed on arrays:

  1. Traversing
  2. Searching
  3. Insertion
  4. Deletion
  5. Sorting
  6. Merging

1. Traversing: It is used to access each data item exactly once so that it can be processed.
E.g.
We have linear array A as below: 

1 2 3 4 5
10 20 30 40 50


Here we will start from beginning and will go till last element and during this process we will access value of each element exactly once as below:

A [1] = 10
A [2] = 20
A [3] = 30
A [4] = 40
A [5] = 50


2. Searching:  It is used to find out the location of the data item if it exists in the given collection of data items.
E.g.
We have linear array A as below:

1 2 3 4 5
15 50 35 20 25


Suppose item to be searched is 20. We will start from beginning and will compare 20 with each element. This process will continue until element is found or array is finished. Here:

1) Compare 20 with 15
20 # 15, go to next element.

2) Compare 20 with 50
20 # 50, go to next element.

3) Compare 20 with 35
20 #35, go to next element.

4) Compare 20 with 20
20 = 20, so 20 is found and its location is 4.

3. Insertion: It is used to add a new data item in the given collection of data items.
E.g.
We have linear array A as below:

1 2 3 4 5
10 20 50 30 15


New element to be inserted is 100 and location for insertion is 3. So shift the elements from 5th location to 3rd location downwards by 1 place. And then insert 100 at 3rd location. It is shown below:



4. Deletion: It is used to delete an existing data item from the given collection of data items.
E.g.
We have linear array A as below:

1 2 3 4 5
10 20 50 40 25 60


The element to be deleted is 50 which is at 3rd location. So shift the elements from 4th to 6th location upwards by 1 place. It is shown below:



After deletion the array will be:
1 2 3 4 5 6
10 20 40 25 60


5. Sorting: It is used to arrange the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data.
E.g.
We have linear array A as below:

1 2 3 4 5
10 50 40 20 30


After arranging the elements in increasing order by using a sorting technique, the array will be:

1 2 3 4 5
10 20 30 40 50


6. Merging: It is used to combine the data items of two sorted files into single file in the sorted form
We have sorted linear array A as below:

1 2 3 4 5 6
10 40 50 80 95 100


And sorted linear array B as below:

1 2 3 4
20 35 45 90


After merging merged array C is as below:

1 2 3 4 5 6 7 8 9 10
10 20 35 40 45 50 80 90 95 100
Share this article   |    Print    |    Article read by 83703 times
Author:
Rohit kakria
I am software developer
Related Articles:
Related Interview Questions: No related interview question