using System;

namespace QTMDataFormat;

public class StatisticsFunctions
{
	public static double GetMedian(double[] sourcedataset, out double[] firsthalf, out double[] secondhalf)
	{
		if (sourcedataset.Length > 0)
		{
			Array.Sort(sourcedataset);
			double result;
			if (sourcedataset.Length > 1)
			{
				if (sourcedataset.Length % 2 == 0)
				{
					int num = sourcedataset.Length / 2;
					firsthalf = new double[num];
					secondhalf = new double[num];
					result = (sourcedataset[num - 1] + sourcedataset[num]) / 2.0;
					for (int i = 0; i < num; i++)
					{
						firsthalf[i] = sourcedataset[i];
					}
					for (int j = num; j < sourcedataset.Length; j++)
					{
						secondhalf[j - num] = sourcedataset[j];
					}
				}
				else
				{
					int num2 = (sourcedataset.Length - 1) / 2;
					result = sourcedataset[num2];
					firsthalf = new double[num2];
					secondhalf = new double[num2];
					for (int k = 0; k < num2; k++)
					{
						firsthalf[k] = sourcedataset[k];
					}
					for (int l = num2 + 1; l < sourcedataset.Length; l++)
					{
						secondhalf[l - num2 - 1] = sourcedataset[l];
					}
				}
			}
			else
			{
				firsthalf = new double[1];
				firsthalf[0] = sourcedataset[0];
				secondhalf = new double[0];
				result = sourcedataset[0];
			}
			return result;
		}
		firsthalf = new double[0];
		secondhalf = new double[0];
		return 0.0;
	}

	public static double GetMedian(double[] sourcedataset)
	{
		double[] firsthalf;
		double[] secondhalf;
		return GetMedian(sourcedataset, out firsthalf, out secondhalf);
	}

	public static double GetFirstQuartile(double[] sourcedataset)
	{
		GetMedian(sourcedataset, out var firsthalf, out var _);
		return GetMedian(firsthalf);
	}

	public static double GetThirdQuartile(double[] sourcedataset)
	{
		GetMedian(sourcedataset, out var _, out var secondhalf);
		return GetMedian(secondhalf);
	}
}
