Operations in Arrays

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.

Traverse is the process of visiting all elements. It can be done simply increasing index by 1. We dont need special algorithm to traverse in array. Though we will talk traversing in coming topics.

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.

Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N.

Following is the algorithm where ITEM is inserted into the Kth position of LA:

1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop


Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all elements of an array.

Following is the algorithm to delete an element available at the Kth position of LA.

1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop


Search Operation

You can perform a search for an array element based on its value or its index.

Following is the algorithm to find an element with a value of ITEM using sequential search.

1. Start
2. Set J = 0
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


Update Operation

Update operation refers to updating an existing element from the array at a given index.

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


DS Arrays Previous Next Implementation of Array Operations