C PROGRAM TO SORT AN ARRAY USING FUNCTION
/** C PROGRAM TO SORT AN ARRAY USING FUNCTION **/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#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.