Open In App

How to Find Median of Numbers in an Array in C?

Last Updated : 25 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

For an odd number of elements in an array, the median is the middle element, and for an even number, it’s the average of the two middle elements. In this article, we will learn how to find median of numbers in an array in C.

The median of an array can be determined is by first sorting the array and then finding the middle element or the average of two middle elements depending on the total number of elements. Let's look at an example:

C
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);  
}

// Function to find the middle element 
float findMedian(int arr[], int n) {
    qsort(arr, n, sizeof(int), compare);

  	// If even, median is the average of the two
  	// middle elements
    if (n % 2 == 0) {
        return (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
    }
  
  	// If odd, median is the middle element
  	else {
        return arr[n / 2];
    }
}

int main() {
    int arr[] = {1, 3, 5, 2, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    float m = findMedian(arr, n);

    printf("%.2f\n", m);
    return 0;
}

Output
22.50

Explanation: The array is first sorted in ascending order using qsort() function with comparator comp(). As the number of elements was odd, the middle element is returned as median.

Using Quick Select Algorithm

Quick Select Algorithm is a variation of quick sort algorithm that tries to find the middle element by arranging the array using the quicksort's partition algorithm and removing the part of array from the consideration in each iteration.

C++
#include <stdio.h>
#include <stdlib.h>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// Function to partition array around pivot element
int partition(int arr[], int l, int r) {
    int pivot = arr[r];
    int i = l;

    for (int j = l; j < r; j++) {
        if (arr[j] < pivot) {
            swap(&arr[i], &arr[j]);
            i++;
        }
    }
    swap(&arr[i], &arr[r]);
    return i;
}

void medianHelper(int arr[], int l, int r, int k, int* a, int* b) {
    if (l <= r) {
        int pi = partition(arr, l, r);

        // If partition index matches k, we found one of the medians
        if (pi == k) {
            *b = arr[pi];
            if (*a != -1)
                return;
        }
        // If partition index matches k - 1, we found the other median
        else if (pi == k - 1) {
            *a = arr[pi];
            if (*b != -1)
                return;
        }

        // If partition index is greater than or equal to k, recurse on the left
        if (pi >= k)
            medianHelper(arr, l, pi - 1, k, a, b);
        else
            // Recurse on the right
            medianHelper(arr, pi + 1, r, k, a, b);
    }
}

// Function to find the median of the array
int findMedian(int arr[], int n) {
    int a = -1, b = -1, m;

    // If n is odd
    if (n % 2 == 1) {
        medianHelper(arr, 0, n - 1, n / 2, &a, &b);
        m = b;
    } else {
        // If n is even
        medianHelper(arr, 0, n - 1, n / 2, &a, &b);
        m = (a + b) / 2;
    }

    return m;
}

int main() {
    int arr[] = {1, 3, 5, 2, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d", findMedian(arr, n));
    return 0;
}

Output
3

Next Article

Similar Reads