[JAVA] Median of the three values

An example of how to find the median of three values in Java

I recently started reading "Algorithms and Data Structures Learned in Java" and solved an exercise to find the median of three values. I will write it down because it seems to be useful later.

	public static void main(String[] args) {
		System.out.println(med3(3, 2, 1)); // [A] a > b > c
		System.out.println(med3(3, 2, 2)); // [B] a > b = c
		System.out.println(med3(3, 1, 2)); // [C] a > c > b
		System.out.println(med3(3, 2, 3)); // [D] a = c > b
		System.out.println(med3(2, 1, 3)); // [E] c > a > b
		System.out.println(med3(3, 3, 2)); // [F] a = b > c
		System.out.println(med3(3, 3, 3)); // [G] a = b = c
		System.out.println(med3(2, 2, 3)); // [H] c > a = b
		System.out.println(med3(2, 3, 1)); // [I] b > a > c
		System.out.println(med3(2, 3, 2)); // [J] b > a = c
		System.out.println(med3(1, 3, 2)); // [K] b > c > a
		System.out.println(med3(2, 3, 3)); // [L] b = c > a
		System.out.println(med3(1, 2, 3)); // [M] c > b > a


	private static int med3(int a, int b, int c) {
		if (a >= b) {
			if (b >= c) {
				return b; // A, B, F, G
			} else if (a <= c) {
				return a; // D, E, H
			} else {
				return c; // C
		} else if (a > c) {
			return a; // I
		} else if (b > c) {
			return c; // J, K
		} else {
			return b; // L, M

There are 13 patterns in the magnitude relationship of the three values, so I called each pattern.

I think there are many other ways to find the median, so please refer to it as an example.

Recommended Posts

Median of the three values
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
[Java] Three features of Java
The idea of jQuery
About the handling of Null
Docker monitoring-explaining the basics of basics-
About the description of Docker-compose.yml
Understand the basics of docker
The play of instantiating java.lang.Void
Explanation of the FizzBuzz problem
The illusion of object orientation
Summary of values returned by the Spliterator characteristics method #java
Switch the version of bundler
Three Reasons to Frustrate Before the Release of a Web Service
About the behavior of ruby Hash # ==
[Java] Delete the elements of List
Continuation: The parable of OOP (omitted)
Qualify only part of the text
Understand the basic mechanism of log4j2.xml
About the basics of Android development
'% 02d' What is the percentage of% 2?
[Rails] Check the contents of the object
Replace the contents of the Jar file
[Java version] The story of serialization
[Swift] Change the textColor of UIDatePicker
[Ruby] See the essence of ArgumentError
The basics of SpringBoot + MyBatis + MySQL
Note on the path of request.getRequestDispatcher
The basic basis of Swift dialogs
I read the source of Integer
This and that of the JDK
Check the migration status of rails
The story of @ViewScoped consuming memory
Filter the fluctuations of raw data
Explaining the columns of Spree :: Taxonomy
A memorandum of the FizzBuzz problem
I read the source of Long
Official logo list of the service
Explain the column of Spree :: Product
Various methods of the String class
About the role of the initialize method
Think about the 7 rules of Optional
Get the ID of automatic numbering
I read the source of Short
I read the source of Byte
Order of processing in the program
I read the source of String
The origin of Java lambda expressions
[Ruby] Display the contents of variables
Filter the result of BindingResult [Spring]
Summary about the introduction of Device
Remote debugging of the Cognos SDK
About the log level of java.util.logging.Logger
How to output the sum of any three numbers, excluding the same value