C tutorial - Operations on Array in C programming language

Programming languages or concepts

Operations on Array:-

There are different operations that can be listed below:-
1). Traversal operation.
2). Searching operation.
3). Insertion operation.
4). Deletion operation.
5). Update operation.
6). Sorting operation.

1). Traversing:-
       Traversing an Array means printing all elements of an Array one by one, we start from the first element and go to the last element.
Psedo code for traversing operation on a linear Array:-
                                                                                      
#include<studio.h>                                                   
void main()                                                                 
{                                                                                    
    int a[4 ] = {1,2,3,4};                                                
    int i;                                                                          
    printf( " the array elements are : \n");               
    for ( i=0; i<4; i++ );                                                 
    {                                                                                 
       printf ( " %d \n ", a[ i ] );                                    
       }                                                                              
 }                                                                                                                                                                          


2) searching operation:-
  Searching is a operation used to find a particular or key data item or element in an Array.
 We can Searching in an unsorted array with the help of traversal of the Array and also we can perform a search for an array element based on it's value or its index.
 in the linear traversal form the first element to the last element can be used to search if a given number is present in an array and can also be used to find it's position if present.
  This is done by comparing each elements with the given element. once the elements is found,the search operation is stopped.
There are two types for searching a item and element in Array:-
1). Sequential search / linear search.
2). Binary search.

3) insertion operation:-
  Insertion operation is used to add a new element in the array. Based on the requirements, a new element can be added at the beginning, mid/any given index of array or end.
    The size of Array is not changed while performing insertion operation.
    An element will be inserted in an array only if it has sufficient space to add it , when array is full, a new element can't be added.
Following can be situation with insertion operation:-
1. Insertion at the beginning of an Array.
2. Insertion of the given index of an Array.
3. Insertion after the given index of an Array.
4. Insertion before the given index of an Array.

4). Deletion operation:-

   Deletion refers to removing an particular elements form the array and reorganizing all elements of an Array.
Spedo code for deletion operation:

#include<stdio.h>.                                                    
void main()                                                                 
int a[ ]={8,2,0,8,6};                                                     
int k=3, n=5;                                                                
int i,j;                                                                            
printf("The original array elements are: \n");.     
for(i=0; i<n; i++)                                                         
{                                                                                    
printf("a[%d]=%d \n",i,a[i]);                                    
}                                                                                    
j=k;                                                                                
while (j<n)                                                                   
a[j-1]=a[j];                                                                    
J=j+1;                                                                            
}                                                                                     
n=n-1;                                                                           
printf("the array elements after deletion:\n");     for(i=0; i<n; i++)                                                         {                                                                                     printf("a[%d]=%d \n", i, a[i] );                                 }                                                                                     }                                                                                    


5). Update operation:-

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

#include<stdio.h>
void main( )
{
   int a[ ] = { 2,4,5,6,7};
   int k=3, n=5, item=10;
   int i,j;
   printf("The array elements are: \n");
   for(i=0; i<n; i++)
   {
      printf("a[%d] = %d \n ", i , a[i]);
    }
     a[k-1]= item;
     printf("the array elements after updation:\n);
for( i=0; i<n ; i++)

    printf("a[%d]=%d \n", i, a[i]);
}
}


6). Sorting operation:-
           This operation is performed to sort an Array into a fixed order. i.e. either ascending or descending.
Different ways of sorting an Array :-
   Below are the different sorting method for Array.
1.Bubble Sorting
2. Selection Sorting
3. Merge Sorting
4. Insertion Sorting
5. Quick Sorting
6. Heap Sorting


For more details
close