Python
- key differences between List and Arrays
- Python collections ChainMap
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python collections
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions in Python
- key differences between List and Arrays
- Program: Calculate Pi in Python
- String Formatting in Python
- Python counters
- python tuples
- Python deque
- Python dictionary
- Python Lists
- python namedtuple
This post presents a Python solution to find the median in a given integer array. The algorithm efficiently calculates the median, which represents the middle value in a sorted array, or the average of the two middle values for arrays with an even number of elements.
By implementing this Python code, you can easily determine the median of any integer array
def find_median(arr):
sorted_arr = sorted(arr)
n = len(sorted_arr)
if n % 2 == 0:
middle1 = sorted_arr[n // 2 - 1]
middle2 = sorted_arr[n // 2]
median = (middle1 + middle2) / 2
else:
median = sorted_arr[n // 2]
return median
usage:
array = [7, 2, 5, 1, 9, 3,7]
median = find_median(array)
print("Median:", median)
Output :
Median: 5