/** 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)
This comment has been removed by a blog administrator.
ReplyDeleteCheck out other awesome sorting programs on CodeTonics
ReplyDeleteInsertion Sort in C
Selection Sort in C
Quick Sort in C
Bubble Sort in C
Heap Sort in C
Merge Sort in C
Radix Sort in C
Good
ReplyDeleteHere is a link for C/C++ programs and pointer programs. This may be useful for you.
C/C++ Programs