Monday, 12 January 2015

INSERT AN ELEMENT IN SORTED ARRAY


                            INSERT AN ELEMENT IN SORTED ARRAY

#include <stdio.h>
void insert_sorted();
int a[50], n, item;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter sorted elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter ITEM to insert: ");
scanf("%d", &item);
insert_sorted();
printf("\n\nAfter insertion:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void insert_sorted()
{
int i = n-1;
while(item<a[i] && i>=0)
{
a[i+1] = a[i];
i--;
}
a[i+1] = item;
n++;
}

OUTPUT




0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More