Monday, 27 October 2014

Array Sorting ( Descending Order )

Non-Recursive C Program:

Aim:
To sort a array of Elements in Ascending order. ( Bubble Sort )

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int A[25],i,j,m,temp;
printf("Enter the size of array:");
scanf("%d",&m);

printf("\nEnter the Elements:");
  for( i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
    }

Printf("The Entered Elements: \n ");
  for( i=0;i<m;i++)
    {
       printf("%d\t",A[i]);
    }

for( i=0;i<m;i++)
{
  for( j=0;j<m-i-1;j++)
    {
       if( A[j] > A[j+1] )
         {
             temp = A[j+1];
              A[j+1] = A[j];
              A[j] = temp;
          }
      }
}

printf("\nArray Elements in Ascending order:");
  for( i=0;i<m;i++)
   {
      printf("%3d",A[i]);
    }
}

Output:
Enter the size of array: 6
Enter the Elements:
82
32
4
17
45
11
The Entered Elements:
82     32     4     17     45     11
Array Elements in Ascending order:
82      45    32    17     11    4

No comments:

Post a Comment