Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

28 Sep 2012

C PROGRAM TO SORT AN ARRAY USING FUNCTION


/** C PROGRAM TO SORT AN ARRAY USING FUNCTION **/
#include<stdio.h>
#include<conio.h>
#define MAX 100     // maximum no of elements of array
int sortArray(int);
int array[MAX];
int main()
{
  int i,size;
  printf("\n>>>> PROGRAM TO SORT ARRAY USING FUNCTION <<<<\n\n");
  printf("\n Enter the size of array: ");
  scanf("%d",&size);
  printf("\n Enter the %d elements of array: \n",size);
  for(i=0;i<size;i++)
  {
   printf("\n array[%d]=",i);
   scanf("%d", &array[i]);
  }
  sortArray(size);    //calling sortArray() function
   printf("\n The Sorted elements of array are:");
   for(i=0;i<size;i++)
   {
     printf(" %d",array[i]);
   }
   getch();
   return 0;
}

sortArray(n)   // function for sorting array elements
{
  int temp=0,i,j;   // temp var is temporary variable used for swapping
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
      if(array[i]>array[j])
      {
        temp=array[i];   //swapping for the array to be sorted
        array[i]= array[j];
        array[j]=temp;
      }
    }
  }
}
Output: ( using GNU GCC Compiler with code::blocks IDE)





4 comments: