#include< stdio.h>
#include< conio.h>
void main()
{
/* Sorting numbers in an array and then moving all even numbers to middle*/
clrscr();
int s[15],n,t,e=0,o,k,ot,tt;
printf("\nEnter Limit : ");
scanf("%d",&n);
printf("\n\nEnter Numbers : \n\n");
for(int i=0;i< n;i++)
{
scanf("%d",&s[i]);
}
printf("\nYour Numbers are : ");
for(i=0;i< n;i++)
{
if((s[i]%2)==0)
e++;
printf("%4d",s[i]);
}
printf("\n\n\nSorted as even numbser at middle : ");
o=n-e;
ot=o/2;
/*Part 1 : Sorting all numbers */
for(i=0;i< n;i++)
{
for(int j=0;j< (n-1-i);j++)
{
if(s[j]>s[j+1])
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
else
continue;
}
}
/*Part 2 : Pushing half odd numbers to end */
for(i=0;i< ot;i++)
{
for(int j=0;j< (n-1-i);j++)
{
if((s[j+1]%2)==0&&(s[j]%2)!=0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
else
continue;
}
}
/*Part 3 : Pushing even numbers to end at remaining place */
tt=n-1-ot;
for(i=0;i< tt;i++)
{
for(int j=0;j< (tt-i);j++)
{
if((s[j+1]%2)!=0&&(s[j]%2)==0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
else
continue;
}
}
for(k=0;k< n;k++)
printf("%4d",s[k]);
getch();
}
0 comments:
Post a Comment