Upper Median

The upper median of a dataset is the kth order statistic, where k = ⌈n/2⌉. In other words, it is the median if there are an odd number of elements, or the larger of the two middle elements if there are an even number of elements.

Java example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int upperMedian(int[] arr) {
  int n = arr.length;
  int k = (n + 1) / 2;
  
  Arrays.sort(arr);
  return arr[k-1];
}

// Example 
int[] arr = {5, 2, 4, 1, 3};
int upperMed = upperMedian(arr); // 4

C++ example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int upperMedian(vector<int> arr) {
  int n = arr.size();
  int k = (n + 1) / 2;

  sort(arr.begin(), arr.end());

  return arr[k-1]; 
}

// Example
vector<int> arr {5, 2, 4, 1, 3};
int upperMed = upperMedian(arr); // 4

Python example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import statistics

def upper_median(arr):
  
  n = len(arr)
  k = (n + 1) // 2
  
  arr.sort()

  return arr[k-1]

# Example
arr = [5, 2, 4, 1, 3] 
upper_med = upper_median(arr) # 4

The upper median divides a dataset into upper and lower halves. It provides insights into the distribution.

The upper median of a dataset is the middle value when the set is sorted and has an odd number of elements. For datasets with an even number of elements, the upper median is the larger of the two middle numbers. It serves as another measure of central tendency, and like the lower median, it can divide a dataset into two nearly equal parts without being skewed by outliers.

Java Code for Upper Median

In Java, the upper median can be found by first sorting the array and then selecting the middle element for an odd-sized array or the larger of the two middle elements for an even-sized array:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Arrays;

public class UpperMedian {
    public static int findUpperMedian(int[] arr) {
        Arrays.sort(arr);
        int n = arr.length;

        if (n % 2 != 0) {
            return arr[n / 2];
        } else {
            return arr[n / 2];
        }
    }

    public static void main(String[] args) {
        int[] array = {5, 3, 9, 1, 4};
        int upperMedian = findUpperMedian(array);
        System.out.println("The upper median is: " + upperMedian);
    }
}
  1. Sort the array using Arrays.sort.
  2. Return the middle element or the larger of the two middle elements based on the number of elements.

C++ Code for Upper Median

In C++, the method to find the upper median is similar to Java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>

int findUpperMedian(int arr[], int size) {
    std::sort(arr, arr + size);

    if (size % 2 != 0) {
        return arr[size / 2];
    } else {
        return arr[size / 2];
    }
}

int main() {
    int arr[] = {5, 3, 9, 1, 4};
    int upperMedian = findUpperMedian(arr, 5);
    std::cout << "The upper median is: " << upperMedian << std::endl;
}
  1. Sort the array with std::sort.
  2. Choose the middle element or the larger of the two middle elements based on the array size.

Python Code for Upper Median

In Python, the code to find the upper median is simple:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def find_upper_median(arr):
    arr.sort();
    n = len(arr);

    if n % 2 != 0:
        return arr[n // 2]
    else:
        return arr[n // 2]

if __name__ == "__main__":
    arr = [5, 3, 9, 1, 4]
    upper_median = find_upper_median(arr)
    print(f"The upper median is: {upper_median}")
  1. Sort the list using sort.
  2. Choose the middle element, whether the list has an odd or even number of elements.

These implementations in Java, C++, and Python show how to find the upper median in an integer set. The primary steps are sorting the dataset and selecting the middle value, taking the size of the dataset into account.