/*** c program to sort and print array using function ***/
#include<stdio.h>
#include<conio.h>
#define MAX 100
void SortArray(int[],int);
main()
{
int arr[MAX],i,size;
printf("\n >>>PROGRAM TO SORT ARRAY ELEMENTS USING
FUNCTION<<<\n \n");
printf("\n Enter the size : ");
scanf("%d",&size); // READING SIZE OF ARRAY FROM USER
printf("\n Enter the elements :\n");
for( i=0;i<size;i++)
{
printf("\n arr[%d]=",i);
scanf("%d",&arr[i]); // reading elements of array from user
}
SortArray(arr,size);
printf("\n The Sorted Elements of Array is: ");
for( i=0;i<size;i++)
{
printf(" %d",arr[i]); //printing sorted array
}
getch(); return 0;
}
void SortArray(int arr1[],int size)
{
int i,j,t;
for(i=size-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(arr1[j]>arr1[j+1])
{
t=arr1[j];
arr1[j]=arr1[j+1];
arr1[j+1]=t;
}
}
}
}
OUTPUT:
This comment has been removed by a blog administrator.
ReplyDelete