package util;
/**
* This class calculates medians. Using quicksort, order the array and return the
* middle value. Also can return median object based on array of values...
*/

public class Median {
	private Median() {}

	static public float getMedianValue(int[] array) {
		float[] temp = new float[array.length] ;
		for (int i = 0; i < array.length; i++) {
			temp[i] = array[i];
		}
		QuickSort.sort(temp) ;
		return medianValue(temp);
	}

	static public float getMedianValue(float[] array) {
		float[] temp = new float[array.length] ;
		System.arraycopy(array,0,temp,0,array.length) ;
		QuickSort.sort(temp);
		return medianValue(temp) ;
	}

	// Pre-requisites: Arrays must have odd number of components, length must be odd
	static public Object getMedianValue(Object[] objects, float[] array, int length) {
		float[] temp = new float[length] ;
		System.arraycopy(array,0,temp,0,length) ;
		QuickSort.sort(objects, temp);
		return medianValue(objects, temp);
	}

	private static float medianValue(float[] array) {
		if (array.length == 1) return array[0];
		if ((array.length % 2) != 0) return array[(array.length-1)/2+1];
		else return (array[array.length/2]+array[array.length/2+1])/2;
	}

	private static Object medianValue(Object []objects, float[] array) {
		if (array.length == 1) return objects[0];
		if ((array.length % 2) != 0) return objects[(array.length-1)/2+1];
		else return objects[array.length/2];
	}

/*	public static void medianFilter(Point []array, int size) {
		float []tempX = new float[size];
		float []tempY = new float[size];

		for (int i = size/2; i < array.length-(size/2); i++) {
			for (int j= 0;j < size; j++) {
				tempX[j] = (float)array[i-size/2+j].x;
				tempY[j] = (float)array[i-size/2+j].y;
			}
			array[i-size/2] = new Point(getMedianValue(tempX), getMedianValue(tempY));
		}
	}	*/
}


