Thursday, 1 June 2017

Sorting an Array element in C ~ GNIITHELP

Program to Sort an Array element

#include<stdio.h>
#include<conio.h>

void sorting(int *x,int y);

void main()
{
  int a[5],b,c;
  clrscr();
  printf("Enter 5 numbers");
  for(b = 0; b < 5; b++)
  {
    scanf("%d",&a[b]);
  }
 sorting(a,5);
 getch();
}

void sorting(int *x,int y)
{
  int i,j,temp;
  for(i = 1; i <= y-1; i++)
  {
    for(j = 0; j < y-i; j++)
    {
      if(*(x+j) >*(x+j+1))
      {
        temp = *(x+j);
        *(x+j) = *(x+j+1);
        *(x+j+1) = temp;
      }
    }
  }
  for(i = 0; i < 5; i++) 
  {
    printf("\t%d",*(x+i));
  }
}
Output
Enter 5 numbers  26  19  21  7  12
      7  12  19  21  26   

No comments:

Post a Comment