import java.util.ArrayList;
import java.util.List;
class MergeSortClass {
public static void main
(String[] args
) { List<Integer> lista = new ArrayList<>();
lista.add(0);
lista.add(3);
lista.add(9);
lista.add(18);
lista.add(5);
lista.add(4);
lista.add(6);
lista.add(10);
ordenar(lista);
System.
out.
println(lista.
toString()); }
public static void ordenar(List<Integer> lista) {
ordenar(lista, 0, lista.size() - 1);
}
private static void ordenar(List<Integer> lista, int inicio, int fim) {
if (inicio >= fim) return;
int meio = (inicio + fim) / 2;
ordenar(lista, inicio, meio);
ordenar(lista, meio + 1, fim);
intercalar(lista, inicio, meio, fim);
}
private static void intercalar(List<Integer> lista, int inicio, int meio, int fim) {
int i = inicio;
int j = meio + 1;
List<Integer> temp = new ArrayList<>(fim - inicio + 1);
while (i <= meio && j <= fim) {
int a = lista.get(i);
int b = lista.get(j);
if (a < b) {
temp.add(a);
i++;
} else {
temp.add(b);
j++;
}
}
while (i <= meio) {
temp.add(lista.get(i));
i++;
}
while (j <= fim) {
temp.add(lista.get(j));
j++;
}
for (int k = 0; k < temp.size(); k++) {
lista.set(k + inicio, temp.get(k));
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKCmNsYXNzIE1lcmdlU29ydENsYXNzIHsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBMaXN0PEludGVnZXI+IGxpc3RhID0gbmV3IEFycmF5TGlzdDw+KCk7CiAgICAgICAgbGlzdGEuYWRkKDApOwogICAgICAgIGxpc3RhLmFkZCgzKTsKICAgICAgICBsaXN0YS5hZGQoOSk7CiAgICAgICAgbGlzdGEuYWRkKDE4KTsKICAgICAgICBsaXN0YS5hZGQoNSk7CiAgICAgICAgbGlzdGEuYWRkKDQpOwogICAgICAgIGxpc3RhLmFkZCg2KTsKICAgICAgICBsaXN0YS5hZGQoMTApOwogICAgICAgIG9yZGVuYXIobGlzdGEpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihsaXN0YS50b1N0cmluZygpKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgb3JkZW5hcihMaXN0PEludGVnZXI+IGxpc3RhKSB7CiAgICAgICAgb3JkZW5hcihsaXN0YSwgMCwgbGlzdGEuc2l6ZSgpIC0gMSk7CiAgICB9CgogICAgcHJpdmF0ZSBzdGF0aWMgdm9pZCBvcmRlbmFyKExpc3Q8SW50ZWdlcj4gbGlzdGEsIGludCBpbmljaW8sIGludCBmaW0pIHsKICAgICAgICBpZiAoaW5pY2lvID49IGZpbSkgcmV0dXJuOwoKICAgICAgICBpbnQgbWVpbyA9IChpbmljaW8gKyBmaW0pIC8gMjsKCiAgICAgICAgb3JkZW5hcihsaXN0YSwgaW5pY2lvLCBtZWlvKTsKICAgICAgICBvcmRlbmFyKGxpc3RhLCBtZWlvICsgMSwgZmltKTsKICAgICAgICBpbnRlcmNhbGFyKGxpc3RhLCBpbmljaW8sIG1laW8sIGZpbSk7CiAgICB9CgogICAgcHJpdmF0ZSBzdGF0aWMgdm9pZCBpbnRlcmNhbGFyKExpc3Q8SW50ZWdlcj4gbGlzdGEsIGludCBpbmljaW8sIGludCBtZWlvLCBpbnQgZmltKSB7CgogICAgICAgIGludCBpID0gaW5pY2lvOwogICAgICAgIGludCBqID0gbWVpbyArIDE7CgogICAgICAgIExpc3Q8SW50ZWdlcj4gdGVtcCA9IG5ldyBBcnJheUxpc3Q8PihmaW0gLSBpbmljaW8gKyAxKTsKCiAgICAgICAgd2hpbGUgKGkgPD0gbWVpbyAmJiBqIDw9IGZpbSkgewogICAgICAgICAgICBpbnQgYSA9IGxpc3RhLmdldChpKTsKICAgICAgICAgICAgaW50IGIgPSBsaXN0YS5nZXQoaik7CiAgICAgICAgICAgIGlmIChhIDwgYikgewogICAgICAgICAgICAgICAgdGVtcC5hZGQoYSk7CiAgICAgICAgICAgICAgICBpKys7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB0ZW1wLmFkZChiKTsKICAgICAgICAgICAgICAgIGorKzsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgd2hpbGUgKGkgPD0gbWVpbykgewogICAgICAgICAgICB0ZW1wLmFkZChsaXN0YS5nZXQoaSkpOwogICAgICAgICAgICBpKys7CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoaiA8PSBmaW0pIHsKICAgICAgICAgICAgdGVtcC5hZGQobGlzdGEuZ2V0KGopKTsKICAgICAgICAgICAgaisrOwogICAgICAgIH0KCiAgICAgICAgZm9yIChpbnQgayA9IDA7IGsgPCB0ZW1wLnNpemUoKTsgaysrKSB7CiAgICAgICAgICAgIGxpc3RhLnNldChrICsgaW5pY2lvLCB0ZW1wLmdldChrKSk7CiAgICAgICAgfQogICAgfQp9