Created
November 15, 2018 20:57
-
-
Save Shtaba09/5b2a1b6d608761d8cf240d61bbaa315e to your computer and use it in GitHub Desktop.
Sort by mediana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task26.task2601; | |
import java.lang.reflect.Array; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
/* | |
Почитать в инете про медиану выборки | |
*/ | |
public class Solution { | |
public static void main(String[] args) { | |
Integer [] inter = {13, 8, 15, 5, 17, 19}; | |
sort(inter); | |
} | |
public static Integer[] sort(Integer[] array) { | |
int mediana; | |
ArrayList<Integer> tmp = new ArrayList<>(); | |
for (int i = 0; i<array.length; i++) { | |
tmp.add(array[i]); | |
} | |
Collections.sort(tmp); | |
//System.out.println(tmp); //proverka | |
if(array.length%2==0){ | |
mediana = (tmp.get((array.length/2)-1) + tmp.get(array.length/2))/2; | |
}else {mediana = tmp.get(((array.length-1)/2));} | |
//System.out.println("Mediana is " + mediana ); //proverka | |
Comparator<Integer> compareByMediana = new Comparator<Integer>() { | |
@Override | |
public int compare(Integer o1, Integer o2) { | |
return Math.abs(mediana-o1)-Math.abs(mediana-o2); | |
} | |
}; | |
Collections.sort(tmp, compareByMediana); | |
for (int i = 0; i<array.length; i++) { | |
array[i]=tmp.get(i); | |
} | |
//System.out.println(tmp); //proverka | |
return array; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task26.task2601" | |
Почитать в инете про медиану выборки | |
Реализуй логику метода sort, который должен сортировать данные в массиве по удаленности от его медианы. | |
Верни отсортированный массив от минимального расстояния до максимального. | |
Если удаленность одинаковая у нескольких чисел, то сортируй их в порядке возрастания. | |
Пример входящего массива: | |
13, 8, 15, 5, 17 | |
медиана - 13 | |
отсортированный масив: | |
13, 15, 17, 8, 5 | |
Требования: | |
1. Программа не должна выводить текст в консоль. | |
2. Программа не должна считывать данные с консоли. | |
3. Класс Solution должен содержать публичный статический метод Integer[] sort(Integer[] array). | |
4. Метод sort(Integer[] array) класса Solution должен сортировать данные в массиве по удаленности от его медианы. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Респект за подсказку бро! Сделал все также, за исключением модуля abs() - без него не работало, подсмотрев тут у тебя получилось сделать.