Program to Insert New value in sorted Array

/* Insert New value in sorted array  */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,pos,x;
clrscr();
printf(“enter how many value in array\n”);
scanf(“%d”,&n);
printf(“Enter %d value in Ascending Order\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Which value to be insert ->”);
scanf(“%d”,&x);
/* Determine the position */
for(i=0;i<n;i++)
if(x<a[i])
{
pos =i;
break;
}
/* insert logic */
/* Shift all data at right side of the array */
for(i=n;i>=pos;i–)
a[i]= a[i-1];
/* insert value at position */
a[pos]=x;
printf(“Your Exist List is :\n “);
for(i=0;i<n;i++)
printf(“%5d”,a[i]);

printf(“\n\nAfter Insert the list is :\n “);
for(i=0;i<=n;i++)
printf(“%5d”,a[i]);

getch();
}

Mohit Arora
Follow me