0% found this document useful (0 votes)
16 views9 pages

Array in Data Structure

nananananan

Uploaded by

Chethan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
16 views9 pages

Array in Data Structure

nananananan

Uploaded by

Chethan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Array in Data Structure

Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations. It is one of the simplest data structures
where each data element can be randomly accessed by using its index
number.

In C programming, they are the derived data types that can store the
primitive type of data such as int, char, double, float, etc. For example, if
we want to store the marks of a student in 6 subjects, then we don't need
to define a different variable for the marks in different subjects. Instead,
we can define an array that can store the marks in each subject at the
contiguous memory locations.

Properties of array

There are some of the properties of an array that are listed as follows -

o Each element in an array is of the same data type and carries the
same size that is 4 bytes.

o Elements in the array are stored at contiguous memory locations


from which the first element is stored at the smallest memory
location.

o Elements of the array can be randomly accessed since we can


calculate the address of each element of the array with the given
base address and the size of the data element.

Representation of an array

We can represent an array in various ways in different programming


languages. As an illustration, let's see the declaration of array in C
language -

As per the above illustration, there are some of the following important
points -

o Index starts with 0.

o The array's length is 10, which means we can store 10 elements.


o Each element in the array can be accessed via its index.

Why are arrays required?

Arrays are useful because -

o Sorting and searching a value in an array is easier.

o Arrays are best to process multiple values quickly and easily.

o Arrays are good for storing multiple values in a single


variable - In computer programming, most cases require storing a
large number of data of a similar type. To store such an amount of
data, we need to define a large number of variables. It would be
very difficult to remember the names of all the variables while
writing the programs. Instead of naming all the variables with a
different name, it is better to define an array and store all the
elements into it.

Memory allocation of an array

As stated above, all the data elements of an array are stored at


contiguous locations in the main memory. The name of the array
represents the base address or the address of the first element in the
main memory. Each element of the array is represented by proper
indexing.

We can define the indexing of an array in the below ways -

1. 0 (zero-based indexing): The first element of the array will be arr[0].

2. 1 (one-based indexing): The first element of the array will be arr[1].

3. n (n - based indexing): The first element of the array can reside at


any random index number.

In the above image, we have shown the memory allocation of an array arr
of size 5. The array follows a 0-based indexing approach. The base
address of the array is 100 bytes. It is the address of arr[0]. Here, the size
of the data type used is 4 bytes; therefore, each element will take 4 bytes
in the memory.

How to access an element from the array?

We required the information given below to access any random element


from the array -

o Base Address of the array.

o Size of an element in bytes.

o Type of indexing, array follows.

The formula to calculate the address to access an array element -

1. Byte address of element A[i] = base address + size * ( i - first index


)

Here, size represents the memory taken by the primitive data types. As an
instance, int takes 2 bytes, float takes 4 bytes of memory space in C
programming.

We can understand it with the help of an example -

Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size
of an element = 2 bytes, find the location of A[-1].

L(A[-1]) = 999 + 2 x [(-1) - (-10)]

= 999 + 18

= 1017

Advertisement

Basic operations

Now, let's discuss the basic operations supported in the array -

o Traversal - This operation is used to print the elements of the array.

o Insertion - It is used to add an element at a particular index.

o Deletion - It is used to delete an element from a particular index.

o Search - It is used to search an element using the given index or by


the value.

o Update - It updates an element at a particular index.

Traversal operation
This operation is performed to traverse through the array elements. It
prints all array elements one after another. We can understand it with the
below program -

1. #include <stdio.h>

2. void main() {

3. int Arr[5] = {18, 30, 15, 70, 12};

4. int i;

5. printf("Elements of the array are:\n");

6. for(i = 0; i<5; i++) {

7. printf("Arr[%d] = %d, ", i, Arr[i]);

8. }

9. }

Output

Insertion operation

This operation is performed to insert one or more elements into the array.
As per the requirements, an element can be added at the beginning, end,
or at any index of the array. Now, let's see the implementation of inserting
an element into the array.

1. #include <stdio.h>

2. int main()

3. {

4. int arr[20] = { 18, 30, 15, 70, 12 };

5. int i, x, pos, n = 5;

6. printf("Array elements before insertion\n");

7. for (i = 0; i < n; i++)

8. printf("%d ", arr[i]);

9. printf("\n");

10.

11. x = 50; // element to be inserted


12. pos = 4;

13. n++;

14.

15. for (i = n-1; i >= pos; i--)

16. arr[i] = arr[i - 1];

17. arr[pos - 1] = x;

18. printf("Array elements after insertion\n");

19. for (i = 0; i < n; i++)

20. printf("%d ", arr[i]);

21. printf("\n");

22. return 0;

23. }

Output

Deletion operation

As the name implies, this operation removes an element from the array
and then reorganizes all of the array elements.

1. #include <stdio.h>

2.

3. void main() {

4. int arr[] = {18, 30, 15, 70, 12};

5. int k = 30, n = 5;

6. int i, j;

7.

8. printf("Given array elements are :\n");

9.

10. for(i = 0; i<n; i++) {


11. printf("arr[%d] = %d, ", i, arr[i]);

12. }

13.

14. j = k;

15.

16. while( j < n) {

17. arr[j-1] = arr[j];

18. j = j + 1;

19. }

20.

21. n = n -1;

22.

23. printf("\nElements of array after deletion:\n");

24.

25. for(i = 0; i<n; i++) {

26. printf("arr[%d] = %d, ", i, arr[i]);

27. }

28. }

Output

Search operation

This operation is performed to search an element in the array based on


the value or index.

1. #include <stdio.h>

2.

3. void main() {

4. int arr[5] = {18, 30, 15, 70, 12};


5. int item = 70, i, j=0 ;

6.

7. printf("Given array elements are :\n");

8.

9. for(i = 0; i<5; i++) {

10. printf("arr[%d] = %d, ", i, arr[i]);

11. }

12. printf("\nElement to be searched = %d", item);

13. while( j < 5){

14. if( arr[j] == item ) {

15. break;

16. }

17.

18. j = j + 1;

19. }

20.

21. printf("\nElement %d is found at %d position", item, j+1);

22. }

Output

Update operation

This operation is performed to update an existing array element located at


the given index.

1. #include <stdio.h>

2.

3. void main() {

4. int arr[5] = {18, 30, 15, 70, 12};


5. int item = 50, i, pos = 3;

6.

7. printf("Given array elements are :\n");

8.

9. for(i = 0; i<5; i++) {

10. printf("arr[%d] = %d, ", i, arr[i]);

11. }

12.

13. arr[pos-1] = item;

14. printf("\nArray elements after updation :\n");

15.

16. for(i = 0; i<5; i++) {

17. printf("arr[%d] = %d, ", i, arr[i]);

18. }

19. }

Output

Complexity of Array operations

Time and space complexity of various array operations are described in


the following table.

Time Complexity

Operation Average Case Worst Case

Access O(1) O(1)

Search O(n) O(n)

Insertion O(n) O(n)


Deletion O(n) O(n)

Space Complexity

In array, space complexity for worst case is O(n).

Advantages of Array

o Array provides the single name for the group of variables of the
same type. Therefore, it is easy to remember the name of all the
elements of an array.

o Traversing an array is a very simple process; we just need to


increment the base address of the array in order to visit each
element one by one.

o Any element in the array can be directly accessed by using the


index.

Disadvantages of Array

o Array is homogenous. It means that the elements with similar data


type can be stored in it.

o In array, there is static memory allocation that is size of an array


cannot be altered.

o There will be wastage of memory if we store less number of


elements than the declared size.

You might also like