
package source.Utility;

//
// Utility Program for TETDM 
// Statistic.java Version 1.00
// Copyright(C) 2007-2018 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//

public class Statistic
{
	public static int max(int a[])
	{
		int tempMax = a[0];

		for(int i=1;i<a.length;i++)
			if(a[i] > tempMax)
				tempMax = a[i];
		
		return tempMax;
	}
	
	public static double max(double a[])
	{
		double tempMax = a[0];
		
		for(int i=1;i<a.length;i++)
			if(a[i] > tempMax)
				tempMax = a[i];
		
		return tempMax;
	}
	
	public static int min(int a[])
	{
		int tempMin = a[0];
		
		for(int i=1;i<a.length;i++)
			if(a[i] < tempMin)
				tempMin = a[i];
		
		return tempMin;
	}
	
	public static double min(double a[])
	{
		double tempMin = a[0];
		
		for(int i=1;i<a.length;i++)
			if(a[i] < tempMin)
				tempMin = a[i];
		
		return tempMin;
	}	
	
	public static double average(int a[])
	{
		double tempTotal = 0.0;
		
		for(int i=0;i<a.length;i++)
			tempTotal += a[i];
		
		return tempTotal/a.length;
	}
	
	public static double average(double a[])
	{
		double tempTotal = 0.0;
		
		for(int i=0;i<a.length;i++)
			tempTotal += a[i];
		
		return tempTotal/a.length;
	}
	
	public static double median(int a[])
	{
		if(a.length % 2 == 1)
			return Qsort.quickselect(a, a.length, (a.length+1)/2);
		else 
			return (Qsort.quickselect(a, a.length, a.length/2) + Qsort.quickselect(a, a.length, a.length/2 + 1))/2;
	}

	public static double median(double a[])
	{
		if(a.length % 2 == 1)
			return Qsort.quickselect(a, a.length, (a.length+1)/2);
		else 
			return (Qsort.quickselect(a, a.length, a.length/2) + Qsort.quickselect(a, a.length, a.length/2 + 1))/2;
	}
	
	public static double stddev(double a[])
	{
		double ave, ave2;
		double a2[] = new double[a.length];
		
		ave = average(a);
		for(int i=0;i<a.length;i++)
			a2[i] = a[i]*a[i];
		ave2 = average(a2);
		
		return Math.sqrt(ave2-ave*ave);		
	}
	
}
