#include <stdio.h>
void ordena(int tamanho, const int *a, int *b) {
int *copia
= (int *) malloc(tamanho
* sizeof(int)); int i;
for (i = 0; i < tamanho; i++) {
copia[i] = a[i];
b[i] = i + 1;
}
int achou_inversao;
do {
achou_inversao = 0;
for (i = 0; i < tamanho - 1; i++) {
if (copia[i] > copia[i + 1]) {
int aux = copia[i];
copia[i] = copia[i + 1];
copia[i + 1] = aux;
aux = b[i];
b[i] = b[i + 1];
b[i + 1] = aux;
achou_inversao = 1;
}
}
} while (achou_inversao);
}
int main(void) {
int a[] = {3, 6, 1, 8};
int b[] = {0, 0, 0, 0};
ordena(4, a, b);
printf("%d %d %d %d", b
[0], b
[1], b
[2], b
[3]); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIG9yZGVuYShpbnQgdGFtYW5obywgY29uc3QgaW50ICphLCBpbnQgKmIpIHsKICAgIGludCAqY29waWEgPSAoaW50ICopIG1hbGxvYyh0YW1hbmhvICogc2l6ZW9mKGludCkpOwogICAgaW50IGk7CiAgICBmb3IgKGkgPSAwOyBpIDwgdGFtYW5obzsgaSsrKSB7CiAgICAgICAgY29waWFbaV0gPSBhW2ldOwogICAgICAgIGJbaV0gPSBpICsgMTsKICAgIH0KCiAgICBpbnQgYWNob3VfaW52ZXJzYW87CiAgICBkbyB7CiAgICAgICAgYWNob3VfaW52ZXJzYW8gPSAwOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCB0YW1hbmhvIC0gMTsgaSsrKSB7CiAgICAgICAgCWlmIChjb3BpYVtpXSA+IGNvcGlhW2kgKyAxXSkgewogICAgICAgIAkJaW50IGF1eCA9IGNvcGlhW2ldOwogICAgICAgIAkJY29waWFbaV0gPSBjb3BpYVtpICsgMV07CiAgICAgICAgCQljb3BpYVtpICsgMV0gPSBhdXg7CiAgICAgICAgCQlhdXggPSBiW2ldOwogICAgICAgIAkJYltpXSA9IGJbaSArIDFdOwogICAgICAgIAkJYltpICsgMV0gPSBhdXg7CiAgICAgICAgCQlhY2hvdV9pbnZlcnNhbyA9IDE7CiAgICAgICAgCX0KICAgICAgICB9CiAgICB9IHdoaWxlIChhY2hvdV9pbnZlcnNhbyk7CiAgICBmcmVlKGNvcGlhKTsKfQoKaW50IG1haW4odm9pZCkgewoJaW50IGFbXSA9IHszLCA2LCAxLCA4fTsKCWludCBiW10gPSB7MCwgMCwgMCwgMH07CglvcmRlbmEoNCwgYSwgYik7CglwcmludGYoIiVkICVkICVkICVkIiwgYlswXSwgYlsxXSwgYlsyXSwgYlszXSk7CglyZXR1cm4gMDsKfQo=