Pages

Sunday, 14 October 2012

WAP to enter an array and Sort it.

  •  WAP to enter an array and Sort it as Following:
 input:1 2 3 4 5 6 7
output: 6 4 2 1 3 5 7

#include <stdio.h>
#include <conio.h>
void main()
{
    int a[15],b[15];
    int goleft=0,bind=0,lind=14;
    //input of array
    printf("Enter the array");
    for(int i=0;i<15;i++)
    scanf("%d",&a[i]);
    //Sorting of array(Bubble Sort)
    for(i=0;i<15;i++)
    {
        for(int j=i+1;j<15;j++)
        {
         if(a[i]>a[j])
         {
            int t=a[i];
            a[i]=a[j];
            t=a[j];
            }
         }
        }

    for(i=14;i>=0;i--)
    {
        if(goleft==0)
          {
                b[lind]=a[i];
                lind--;goleft=1;
          }
          else
          {
            b[bind]=a[i];
            bind++;goleft=0;
            }
          }
          for(i=0;i<15;i++)
          printf("%d",b[i]);
        }

2 comments:

  1. yeh kis typ ka sorting hai yaar...

    ReplyDelete
  2. its a type of sorting to start from middle,left then right...

    ReplyDelete