![]() |
VOOZH | about |
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:
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.
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.
3