Given an array, I was asked to sort it in a way so that the smallest values is first the, largest value second, the second smallest value third and so on. However when I input the values I do not get the required output.
Advise is appreciated as I have an exam today.
#include<stdio.h>
int main()
{
int i,j,k,a[6],temp,min;
for(i=0;i<6;i++)
scanf("%d",&a[i]);
for(j=0;j<6;j++)
{
if(j%2==0)
{
min=a[j];
for(k=j;k<6;k++)
{
if(a[k++]<min)
min=a[k++];
}
temp=a[j];
a[j]=min;
min=temp;
}
else
{
min=a[j];
for(k=j;k<6;k++)
{
if(a[k++]>min)
min=a[k++];
}
temp=a[j];
a[j]=min;
min=temp;
}
printf("%d ",a[j]);
}
}