void mergeInt(int v[], int inicio, int fim) {
int meio = (inicio + fim) / 2;
int i = inicio;
int j = meio + 1;
int tam = fim - inicio + 1;
int *aux
= malloc(sizeof(int) * tam
);
int k = 0;
while (i <= meio || j <= fim) {
if (i > meio) {
aux[k] = v[j];
j++;
} else if (j > fim) {
aux[k] = v[i];
i++;
} else if (v[i] < v[j]) {
aux[k] = v[i];
i++;
} else {
aux[k] = v[j];
j++;
}
k++;
}
for (int l = 0; l < tam; l++) {
v[inicio + l] = aux[l];
}
}
void mergeSortInt(int v[], int inicio, int fim) {
int meio = (inicio + fim) / 2;
if (inicio < fim) {
mergeSortInt(v, inicio, meio);
mergeSortInt(v, meio + 1, fim);
mergeInt(v, inicio, fim);
}
}
void mergeSort(int v[], int tamanho) {
mergeSortInt(v, 0, tamanho);
}
int main() {
int a[] = {10, 2, 7, 1, 4, 9, 3, 8, 0, 5, 6};
mergeSort(a, 11);
for (int i = 0; i < 11; i++) {
}
}
dm9pZCBtZXJnZUludChpbnQgdltdLCBpbnQgaW5pY2lvLCBpbnQgZmltKSB7CiAgICBpbnQgbWVpbyA9IChpbmljaW8gKyBmaW0pIC8gMjsKICAgIGludCBpID0gaW5pY2lvOwogICAgaW50IGogPSBtZWlvICsgMTsKCiAgICBpbnQgdGFtID0gZmltIC0gaW5pY2lvICsgMTsKICAgIGludCAqYXV4ID0gbWFsbG9jKHNpemVvZihpbnQpICogdGFtKTsKCiAgICBpbnQgayA9IDA7CiAgICB3aGlsZSAoaSA8PSBtZWlvIHx8IGogPD0gZmltKSB7CiAgICAgICAgaWYgKGkgPiBtZWlvKSB7CiAgICAgICAgICAgIGF1eFtrXSA9IHZbal07CiAgICAgICAgICAgIGorKzsKICAgICAgICB9IGVsc2UgaWYgKGogPiBmaW0pIHsKICAgICAgICAgICAgYXV4W2tdID0gdltpXTsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0gZWxzZSBpZiAodltpXSA8IHZbal0pIHsKICAgICAgICAgICAgYXV4W2tdID0gdltpXTsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGF1eFtrXSA9IHZbal07CiAgICAgICAgICAgIGorKzsKICAgICAgICB9CiAgICAgICAgaysrOwogICAgfQoKICAgIGZvciAoaW50IGwgPSAwOyBsIDwgdGFtOyBsKyspIHsKICAgICAgICB2W2luaWNpbyArIGxdID0gYXV4W2xdOwogICAgfQoKICAgIGZyZWUoYXV4KTsKfQoKdm9pZCBtZXJnZVNvcnRJbnQoaW50IHZbXSwgaW50IGluaWNpbywgaW50IGZpbSkgewogICAgaW50IG1laW8gPSAoaW5pY2lvICsgZmltKSAvIDI7CgogICAgaWYgKGluaWNpbyA8IGZpbSkgewogICAgICAgIG1lcmdlU29ydEludCh2LCBpbmljaW8sIG1laW8pOwogICAgICAgIG1lcmdlU29ydEludCh2LCBtZWlvICsgMSwgZmltKTsKICAgICAgICBtZXJnZUludCh2LCBpbmljaW8sIGZpbSk7CiAgICB9Cn0KCnZvaWQgbWVyZ2VTb3J0KGludCB2W10sIGludCB0YW1hbmhvKSB7CiAgICBtZXJnZVNvcnRJbnQodiwgMCwgdGFtYW5obyk7Cn0KCmludCBtYWluKCkgewogICAgaW50IGFbXSA9IHsxMCwgMiwgNywgMSwgNCwgOSwgMywgOCwgMCwgNSwgNn07CiAgICBtZXJnZVNvcnQoYSwgMTEpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAxMTsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCIlZCAiLCBhW2ldKTsKICAgIH0KfQ==