banner image

zoho c program to sort the elements in odd positions in descending order and elements in ascending order

Write a program to sort the elements in odd positions in descending order and elements in ascending order

Eg 1: Input: 13,2 4,15,12,10,5
        Output: 13,2,12,10,5,15,4
Eg 2: Input: 1,2,3,4,5,6,7,8,9
        Output: 9,2,7,4,5,6,3,8,1 
#include<stdio.h>
int main()
{
    int n,i,k=0,j,t=0,t1;
    scanf("%d",&n);
    int a[n],b[n/2],c[n/2];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(i%2==0)
        {b[k]=a[i];++k;}//printf("%d ",a[i]);
        else
        {c[t]=a[i];++t;} 
    }
    for(i=0;i<t;i++)
     for(j=0;j<t;j++)
          if(c[i]<c[j])
           {
               t1=c[i];
               c[i]=c[j];
               c[j]=t1;
     for(i=0;i<k;i++)
      for(j=0;j<k;j++)
         if(b[i]>b[j])
           {
               t=b[i];
               b[i]=b[j];
               b[j]=t;
           }   
   k=0;t=0;
    for(i=0;i<n;i++)
    {
        if(i%2==0)
           printf("%d ",b[k]);++k;
       else
            printf("%d ",c[t++]);
    }
    return 0;
}

zoho c program to sort the elements in odd positions in descending order and elements in ascending order zoho c program to sort the elements in odd positions in descending order and elements in ascending order Reviewed by astin salvi on 11:20 AM Rating: 5

No comments:

Powered by Blogger.