Why does he multiply v1 and v2 with themselves and then multiply the result by 100?
public static Integer[] sort(Integer[] array) {
final double mediana = getMediana(array);
Arrays.sort(array, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
double v1 = o1.intValue() - mediana;
double v2 = o2.intValue() - mediana;
return (int) ((v1 * v1 - v2 * v2) * 100);
}
});
package com.codegym.task.task26.task2601;
/*
Read online about the median of a sample
*/
import java.util.Arrays;
import java.util.Comparator;
public class Solution {
public static void main(String[] args) {
Integer[] array = new Integer[]{13,8,15,5,17};
sort(array);
for (Integer integer : array) System.out.println(integer);
}
public static Integer[] sort(Integer[] array) {
// Implement the logic here
//identify the median
double median = getMedian(array);
//System.out.println(median);
//least difference from the median first. if difference identical, sort ascending
Comparator<Integer> medianComp = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return (int) (o1.intValue() - median - o2.intValue() - median);
}
};
Arrays.sort(array);
return array;
}
public static double getMedian(Integer[] array){
// First we sort the array
Arrays.sort(array);
// check for even case
if (array.length % 2 != 0)
return (double)array[array.length / 2];
return (double)(array[array.length / 2 - 1] + array[array.length / 2]) / 2.0;
}
}