C program to Insert an Element in the Sorted Array

«
»

This is a C Program to insert an element in a specified position in a given array.

Problem Description

This program implements a one dimentional array, sorts it and then takes a user input and inserts the desired element in the specified position of a one dimentional array and print all the elements of the array with a +1 increment in the array size.

Problem Solution

1. Declare a one dimentional array of some fixed capacity.
2. Take size of the array as input from users, which must be at least one less than array’s capacity.
3. Define array elements, taking each element as input from users.
4. Sort the array elements.
5. Now, take a value from users, which needs to be inserted inside this array.
6. Select a suitable position for the new element by comparing the new element to the array elements and insert it in that position.
7. Print all the elements. Now, the array size would be previous array size+1.
8. Exit

Program/Source Code

Here is source code of the C program to insert an element in a specified position in a given array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2. /*
  3.  * C program to insert a particular element in a specified position
  4.  * in a given array
  5.  */
  6.  
  7.     #include <stdio.h>
  8.     void main()
  9.     {
  10.         int array[10];
  11.         int i, j, n, m, temp, key, pos;
  12.  
  13.         printf("Enter how many elements \n");
  14.         scanf("%d", &n);
  15.         printf("Enter the elements \n");
  16.  
  17.         for (i = 0; i < n; i++)
  18.         {
  19.             scanf("%d", &array[i]);
  20.         }
  21.  
  22.         printf("Input array elements are \n");
  23.         for (i = 0; i < n; i++)
  24.         {
  25.             printf("%d\n", array[i]);
  26.         }
  27.  
  28. 	// Sorting the elements of the array
  29.         for (i = 0; i < n; i++)
  30.         {
  31.             for (j = i + 1; j < n; j++)
  32.             {
  33.                 if (array[i] > array[j])
  34.                 {
  35.                     temp = array[i];
  36.                     array[i] = array[j];
  37.                     array[j] = temp;
  38.                 }
  39.              }
  40.         }
  41.  
  42.         printf("Sorted list is \n");
  43.         for (i = 0; i < n; i++)
  44.         {
  45.             printf("%d\n", array[i]);
  46.         }
  47.  
  48.         printf("Enter the element to be inserted \n");
  49.         scanf("%d", &key);
  50.  
  51.         for (i = 0; i < n; i++)
  52.         {
  53.             if (key < array[i])
  54.             {
  55.                 pos = i;
  56.                 break;
  57.             }
  58.             if (key > array[n-1])
  59.             { 
  60.                 pos = n;
  61.                 break;
  62.             }
  63.         }
  64.         if (pos != n)
  65.         {
  66.             m = n - pos + 1 ;
  67.             for (i = 0; i <= m; i++)
  68.             {
  69.                 array[n - i + 2] = array[n - i + 1] ;
  70.             }
  71.         }
  72.  
  73.         array[pos] = key;
  74.  
  75.         printf("Final list is \n");
  76.         for (i = 0; i < n + 1; i++)
  77.         {
  78.             printf("%d\n", array[i]);
  79.         }
  80.  
  81.     }
Program Explanation

1. Declare a one dimentional array, a of some fixed capacity, 10.
2. Take an input for size of the array, n. The size of the array must be at least one smaller than array’s capacity, 10.
3. Using for loop, fill the array with elements to its size.
4. Print all the elements of the array.
5. Sort the elements of the array in ascending order. In this case, we have used Insertion sort.
6. Now, take a value from users, which needs to be inserted inside array.
7. Run a for loop from 0 to arraySize-1, checking and comparing each element of array
i) If new element is smaller than array element, then the position is array element is saved.
ii) If new element is bigger than the last element of array (biggest element of array), then the size of array is saved.
8. Position saved is where the new element will be inserted.
9. All the elements at that position in the array and after that are shifted backwards by 1.
10. Print all the elements of the new array.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
Enter how many elements
5
Enter the elements
76
90
56
78
12
Input array elements are
76
90
56
78
12
Sorted list is
12
56
76
78
90
Enter the element to be inserted
61
Final list is
12
56
61
76
78
90

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Take C Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.