Insert any element into an array and then sort the array
#include<stdio.h>
#include<conio.h>
main()
{
int a[15],n,m,i,j,temp;
clrscr();
printf("\n How many elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element no. %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n Original unsorted elements are:");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n enter the element to be inserted : ");
scanf("%d",&a[n]);
m=n+1; // n is increased b'coz of entry of new element
for(i=0;i<m;i++) // Bubble Sort
for(j=0;j<m-1;j++)
if(a[j]<=a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\n Sorted elements of the array are:");
printf("\n");
for(i=0;i<m;i++)
printf(" %d ",a[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int a[15],n,m,i,j,temp;
clrscr();
printf("\n How many elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element no. %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n Original unsorted elements are:");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n enter the element to be inserted : ");
scanf("%d",&a[n]);
m=n+1; // n is increased b'coz of entry of new element
for(i=0;i<m;i++) // Bubble Sort
for(j=0;j<m-1;j++)
if(a[j]<=a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\n Sorted elements of the array are:");
printf("\n");
for(i=0;i<m;i++)
printf(" %d ",a[i]);
getch();
}
No comments:
Post a Comment