C Programming Code Examples C > Sorting Searching Code Examples Median Program In C programming Median Program In C programming A median value is the value at the center of a sorted list. To median we need to sort the list in ascending or descending order. For Example take the list of 3, 5, 2, 7, 3 as our input list. To find out median, first we re-order it as 2, 3, 3, 5, 7. and we find that at location 3 ((5+1)/2) is 3. So the value of median in this list is 3. #include <stdio.h> void swap(int *p,int *q) { int t; t=*p; *p=*q; *q=t; } void sort(int a[],int n) { int x,j,temp; for(x=0;x<n-1;x++) { for(j=0;j<n-x-1;j++) { if(a[j]>a[j+1]) swap(&a[j],&a[j+1]); } } } int main() { int a[] = {6,3,8,5,1}; int n = 5; int sum,x; sort(a,n); n = (n+1) / 2 - 1; // -1 as array indexing in C starts from 0 printf("Median = %d ", a[n]); return 0; } procedure median() Array A Size N SORT(A) middle = (N + 1) / 2 DISPLAY A[middle] as median end procedure