- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Data Structure
- DSA - Doubly Linked List Data Structure
- DSA - Circular Linked List Data Structure
- Stack & Queue
- DSA - Stack Data Structure
- DSA - Expression Parsing
- DSA - Queue Data Structure
- Searching Algorithms
- DSA - Searching Algorithms
- DSA - Linear Search Algorithm
- DSA - Binary Search Algorithm
- DSA - Interpolation Search
- DSA - Jump Search Algorithm
- DSA - Exponential Search
- DSA - Fibonacci Search
- DSA - Sublist Search
- DSA - Hash Table
- Sorting Algorithms
- DSA - Sorting Algorithms
- DSA - Bubble Sort Algorithm
- DSA - Insertion Sort Algorithm
- DSA - Selection Sort Algorithm
- DSA - Merge Sort Algorithm
- DSA - Shell Sort Algorithm
- DSA - Heap Sort
- DSA - Bucket Sort Algorithm
- DSA - Counting Sort Algorithm
- DSA - Radix Sort Algorithm
- DSA - Quick Sort Algorithm
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- DSA - Spanning Tree
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Tries
- DSA - Heap Data Structure
- Recursion
- DSA - Recursion Algorithms
- DSA - Tower of Hanoi Using Recursion
- DSA - Fibonacci Series Using Recursion
- Divide and Conquer
- DSA - Divide and Conquer
- DSA - Max-Min Problem
- DSA - Strassen's Matrix Multiplication
- DSA - Karatsuba Algorithm
- Greedy Algorithms
- DSA - Greedy Algorithms
- DSA - Travelling Salesman Problem (Greedy Approach)
- DSA - Prim's Minimal Spanning Tree
- DSA - Kruskal's Minimal Spanning Tree
- DSA - Dijkstra's Shortest Path Algorithm
- DSA - Map Colouring Algorithm
- DSA - Fractional Knapsack Problem
- DSA - Job Sequencing with Deadline
- DSA - Optimal Merge Pattern Algorithm
- Dynamic Programming
- DSA - Dynamic Programming
- DSA - Matrix Chain Multiplication
- DSA - Floyd Warshall Algorithm
- DSA - 0-1 Knapsack Problem
- DSA - Longest Common Subsequence Algorithm
- DSA - Travelling Salesman Problem (Dynamic Approach)
- Approximation Algorithms
- DSA - Approximation Algorithms
- DSA - Vertex Cover Algorithm
- DSA - Set Cover Problem
- DSA - Travelling Salesman Problem (Approximation Approach)
- Randomized Algorithms
- DSA - Randomized Algorithms
- DSA - Randomized Quick Sort Algorithm
- DSA - Karger’s Minimum Cut Algorithm
- DSA - Fisher-Yates Shuffle Algorithm
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Array Insertions
In the previous section, we have learnt how the insertion operation works. It is not always necessary that an element is inserted at the end of an array. Following can be a situation with array insertion −
- Insertion at the beginning of an array
- Insertion at the given index of an array
- Insertion after the given index of an array
- Insertion before the given index of an array
Insertion at the Beginning of an Array
When the insertion happens at the beginning, it causes all the existing data items to shift one step downward. Here, we design and implement an algorithm to insert an element at the beginning of an array.
Algorithm
We assume A is an array with N elements. The maximum numbers of elements it can store is defined by MAX. We shall first check if an array has any empty space to store any element and then we proceed with the insertion process.
begin IF N = MAX, return ELSE N = N + 1 For All Elements in A Move to next adjacent location A[FIRST] = New_Element end
Implementation in C
#include <stdio.h> #define MAX 5 void main() { int array[MAX] = {2, 3, 4, 5}; int N = 4; // number of elements in array int i = 0; // loop variable int value = 1; // new data element to be stored in array // print array before insertion printf("Printing array before insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d \n", i, array[i]); } // now shift rest of the elements downwards for(i = N; i >= 0; i--) { array[i+1] = array[i]; } // add new element at first position array[0] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d\n", i, array[i]); } }
Output
This program should yield the following output −
Printing array before insertion − array[0] = 2 array[1] = 3 array[2] = 4 array[3] = 5 Printing array after insertion − array[0] = 0 array[1] = 2 array[2] = 3 array[3] = 4 array[4] = 5
Insertion at the Given Index of an Array
In this scenario, we are given the exact location (index) of an array where a new data element (value) needs to be inserted. First we shall check if the array is full, if it is not, then we shall move all data elements from that location one step downward. This will make room for a new data element.
Algorithm
We assume A is an array with N elements. The maximum numbers of elements it can store is defined by MAX.
begin IF N = MAX, return ELSE N = N + 1 SEEK Location index For All Elements from A[index] to A[N] Move to next adjacent location A[index] = New_Element end
Implementation in C
#include <stdio.h> #define MAX 5 void main() { int array[MAX] = {1, 2, 4, 5}; int N = 4; // number of elements in array int i = 0; // loop variable int index = 2; // index location to insert new value int value = 3; // new data element to be inserted // print array before insertion printf("Printing array before insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d \n", i, array[i]); } // now shift rest of the elements downwards for(i = N; i >= index; i--) { array[i+1] = array[i]; } // add new element at first position array[index] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d\n", i, array[i]); } }
Output
If we compile and run the above program, it will produce the following result −
Printing array before insertion − array[0] = 1 array[1] = 2 array[2] = 4 array[3] = 5 Printing array after insertion − array[0] = 1 array[1] = 2 array[2] = 3 array[3] = 4 array[4] = 5
Insertion After the Given Index of an Array
In this scenario we are given a location (index) of an array after which a new data element (value) has to be inserted. Only the seek process varies, the rest of the activities are the same as in the previous example.
Algorithm
We assume A is an array with N elements. The maximum numbers of elements it can store is defined by MAX.
begin IF N = MAX, return ELSE N = N + 1 SEEK Location index For All Elements from A[index + 1] to A[N] Move to next adjacent location A[index + 1] = New_Element end
Implementation in C
#include <stdio.h> #define MAX 5 void main() { int array[MAX] = {1, 2, 4, 5}; int N = 4; // number of elements in array int i = 0; // loop variable int index = 1; // index location after which value will be inserted int value = 3; // new data element to be inserted // print array before insertion printf("Printing array before insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d \n", i, array[i]); } // now shift rest of the elements downwards for(i = N; i >= index + 1; i--) { array[i + 1] = array[i]; } // add new element at first position array[index + 1] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d\n", i, array[i]); } }
Output
If we compile and run the above program, it will produce the following result −
Printing array before insertion − array[0] = 1 array[1] = 2 array[2] = 4 array[3] = 5 Printing array after insertion − array[0] = 1 array[1] = 2 array[2] = 3 array[3] = 4 array[4] = 5
Insertion Before the Given Index of an Array
In this scenario we are given a location (index) of an array before which a new data element (value) has to be inserted. This time we seek till index-1 i.e., one location ahead of given index, rest of the activities are same as in previous example.
Algorithm
We assume A is an array with N elements. The maximum numbers of elements it can store is defined by MAX.
begin IF N = MAX, return ELSE N = N + 1 SEEK Location index For All Elements from A[index - 1] to A[N] Move to next adjacent location A[index - 1] = New_Element end
Implementation in C
#include <stdio.h> #define MAX 5 void main() { int array[MAX] = {1, 2, 4, 5}; int N = 4; // number of elements in array int i = 0; // loop variable int index = 3; // index location before which value will be inserted int value = 3; // new data element to be inserted // print array before insertion printf("Printing array before insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d \n", i, array[i]); } // now shift rest of the elements downwards for(i = N; i >= index + 1; i--) { array[i + 1] = array[i]; } // add new element at first position array[index + 1] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion −\n"); for(i = 0; i < N; i++) { printf("array[%d] = %d\n", i, array[i]); } }
Output
If we compile and run the above program, it will produce the following result −
Printing array before insertion − array[0] = 1 array[1] = 2 array[2] = 4 array[3] = 5 Printing array after insertion − array[0] = 1 array[1] = 2 array[2] = 4 array[3] = 5 array[4] = 3