Program to sort an array in odd even odd fashion
#include < stdio.h>
#include < conio.h>
int a[10];
void swap(int i);
main()
{
int e,o=0,x,t,i,j,k;
clrscr();
printf("Enter the array");
for(i=0;i < 10;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==1)
o++;
}
for(j=0;j < 10;j++)
{
for(i=0;i < 9-j;i++)
{
if(a[i]>a[i+1])
swap(i);
}}
for(i=0;i < 10;i++)
{
for(j=0;j < 9;j++)
{
if((a[j]%2==1)&&(a[j+1]%2==0))
swap(j);
}}
x=o/2;
e=10-o;
for(j=0;j < x;j++)
{
t=a[e+j];
for(i=e+j;i>j;i--)
a[i]=a[i-1];
a[j]=t;
}
printf("\nSorted Array\n");
for(i=0;i < 10;i++)
printf("%d\n",a[i]);
getch();
}
void swap(int i)
{
int t;
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts
sort even and odd terms of an array
Program to sort even and odd terms of an array
#include < stdio.h>
#include < conio.h>
main()
{
int t,a[10],i,j,k;
clrscr();
printf("Enter the array");
for(i=0;i < 10;i++)
{
scanf("%d",&a[i]);
}
for(j=0;j < 10;j++)
{
for(i=0;i < 9-j;i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}}
for(i=0;i < 10;i++)
{
for(j=0;j < 9;j++)
{
if((a[j]%2==0)&&(a[j]%2)!=(a[j+1]%2))
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}}
printf("\nSorted Array\n");
for(i=0;i < 10;i++)
printf("%d\n",a[i]);
getch();
}
#include < stdio.h>
#include < conio.h>
main()
{
int t,a[10],i,j,k;
clrscr();
printf("Enter the array");
for(i=0;i < 10;i++)
{
scanf("%d",&a[i]);
}
for(j=0;j < 10;j++)
{
for(i=0;i < 9-j;i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}}
for(i=0;i < 10;i++)
{
for(j=0;j < 9;j++)
{
if((a[j]%2==0)&&(a[j]%2)!=(a[j+1]%2))
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}}
printf("\nSorted Array\n");
for(i=0;i < 10;i++)
printf("%d\n",a[i]);
getch();
}
Labels:
c,
cp lab,
even and odd,
programming,
sort