Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Introduction

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. 

Following are the important terms to understand the concept of Array:

  • Element: Each item stored in an array is called an element.
  • Index: Each location of an element in an array has a numerical index, which is used to identify the element.

Array Representation

Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE)Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE)As per the above illustration, following are the important points to be considered.

  • Index starts with θ.
  • Array length is 1θ which means it can store 1θ elements.
  • Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.

Basic Operations

Following are the basic operations supported by an array.

  • Traverse: print all the array elements one by one.
  • Insertion: Adds an element at the given index.
  • Deletion: Deletes an element at the given index.
  • Search: Searches an element using the given index or by the value.
  • Update: Updates an element at the given index.

In C, when an array is initialized with size, then it assigns defaults values to its elements in following order.
Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE)

  1. Traverse Operation
    This operation is to traverse through the elements of an array.
    Example:
    Following program traverses and prints the elements of an array:
    #include <stdio.h>
    main() {
       int LA[] = {1,3,5,7,8};
       int item = 1θ, k = 3, n = 5;
       int i = θ, j = n;  
       printf("The original array elements are :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
    }
    When we compile and execute the above program, it produces the following result −
    Output
    The original array elements are:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 5
    LA[3] = 7
    LA[4] = 8
  2. Insertion Operation
    Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
    Here, we see a practical implementation of insertion operation, where we add data at the end of the array −
    Example:
    Following is the implementation of the above algorithm:
    #include <stdio.h>
    main() {
       int LA[] = {1,3,5,7,8};
       int item = 1θ, k = 3, n = 5;
       int i = θ, j = n;
       printf("The original array elements are :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
       n = n + 1;
       while( j >= k) {
          LA[j+1] = LA[j];
          j = j - 1;
       }
       LA[k] = item;
       printf("The array elements after insertion :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
    }
    When we compile and execute the above program, it produces the following result −
    Output
    The original array elements are:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 5
    LA[3] = 7
    LA[4] = 8
    The array elements after insertion:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 5
    LA[3] = 1θ
    LA[4] = 7
    LA[5] = 8
  3. Deletion Operation
    Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
    Algorithm
    Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA.
    (i) Start
    (ii) Set J = K
    (iii) Repeat steps 4 and 5 while J < N
    (iv) Set LA[J] = LA[J + 1]
    (v) Set J = J+1
    (vi) Set N = N-1
    (vii) Stop
    Example:
    Following is the implementation of the above algorithm:
    #include <stdio.h>
    void main() {
       int LA[] = {1,3,5,7,8};
       int k = 3, n = 5;
       int i, j;
       printf("The original array elements are :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
       j = k;
       while( j < n) {
          LA[j-1] = LA[j];
          j = j + 1;
       }
       n = n -1;
       printf("The array elements after deletion :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
    }
    When we compile and execute the above program, it produces the following result −
    Output
    The original array elements are:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 5
    LA[3] = 7
    LA[4] = 8
    The array elements after deletion:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 7
    LA[3] = 8
  4. Search Operation
    You can perform a search for an array element based on its value or its index.
    Algorithm
    Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search.
    1. Start
    2. Set J = θ
    3. Repeat steps 4 and 5 while J < N
    4. IF LA[J] is equal ITEM THEN GOTO STEP 6
    5. Set J = J +1
    6. PRINT J, ITEM
    7. Stop
    Example:
    Following is the implementation of the above algorithm:
    #include <stdio.h>
    void main() {
       int LA[] = {1,3,5,7,8};
       int item = 5, n = 5;
       int i = θ, j = θ;
       printf("The original array elements are :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
       while( j < n){
          if( LA[j] == item ) {
             break;
          }
          j = j + 1;
       }
       printf("Found element %d at position %d\n", item, j+1);
    }
    When we compile and execute the above program, it produces the following result −
    Output
    The original array elements are:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 5
    LA[3] = 7
    LA[4] = 8
    Found element 5 at position 3
  5. Update Operation
    Update operation refers to updating an existing element from the array at a given index.
    Algorithm
    Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA.
    1. Start
    2. Set LA[K-1] = ITEM
    3. Stop
    Example
    Following is the implementation of the above algorithm:
    #include <stdio.h>
    void main() {
       int LA[] = {1,3,5,7,8};
       int k = 3, n = 5, item = 1θ;
       int i, j;
       printf("The original array elements are :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
       LA[k-1] = item;
       printf("The array elements after updation :\n");
       for(i = θ; i<n; i++) {
          printf("LA[%d] = %d \n", i, LA[i]);
       }
    }
    When we compile and execute the above program, it produces the following result −
    Output
    The original array elements are:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 5
    LA[3] = 7
    LA[4] = 8
    The array elements after updation:
    LA[θ] = 1
    LA[1] = 3
    LA[2] = 1θ
    LA[3] = 7
    LA[4] = 8
The document Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

practice quizzes

,

Extra Questions

,

Semester Notes

,

Summary

,

shortcuts and tricks

,

Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE)

,

video lectures

,

Sample Paper

,

study material

,

Previous Year Questions with Solutions

,

MCQs

,

Exam

,

past year papers

,

Free

,

Objective type Questions

,

pdf

,

Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE)

,

ppt

,

Operation on Arrays | Programming and Data Structures - Computer Science Engineering (CSE)

,

Important questions

,

mock tests for examination

;