#include <stdio.h>
#include <stdlib.h>
struct rank_pair {
double val;
size_t ind;
};
int cmp_rank_pair(const void* a, const void* b) {
struct rank_pair *lhs = (struct rank_pair*)a;
struct rank_pair *rhs = (struct rank_pair*)b;
return lhs->val < rhs->val ? -1 : (lhs->val > rhs->val ? 1 : 0);
}
void rank(double a[], int r[], size_t n) {
struct rank_pair
*tmp
= malloc(n
*sizeof(struct rank_pair
)); for (int i = 0 ; i != n ; i++) {
tmp[i].val = a[i];
tmp[i].ind = i;
}
qsort(tmp
, n
, sizeof(struct rank_pair
), cmp_rank_pair
); for (int i = 0 ; i != n ; i++) {
r[tmp[i].ind] = i+1;
}
}
int main(void) {
const size_t N = 10;
double a[] = {0.25, 0.54, 0.38, 0.32, 0.49, 0.06, 0.41, 0.21, 0.98, 0.23};
int r[N];
rank(a, r, N);
for (int i = 0 ; i != N ; i++) {
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnN0cnVjdCByYW5rX3BhaXIgewoJZG91YmxlIHZhbDsKCXNpemVfdCBpbmQ7Cn07CgppbnQgY21wX3JhbmtfcGFpcihjb25zdCB2b2lkKiBhLCBjb25zdCB2b2lkKiBiKSB7CglzdHJ1Y3QgcmFua19wYWlyICpsaHMgPSAoc3RydWN0IHJhbmtfcGFpciopYTsKCXN0cnVjdCByYW5rX3BhaXIgKnJocyA9IChzdHJ1Y3QgcmFua19wYWlyKiliOwoJcmV0dXJuIGxocy0+dmFsIDwgcmhzLT52YWwgPyAtMSA6IChsaHMtPnZhbCA+IHJocy0+dmFsID8gMSA6IDApOwp9Cgp2b2lkIHJhbmsoZG91YmxlIGFbXSwgaW50IHJbXSwgc2l6ZV90IG4pIHsKCXN0cnVjdCByYW5rX3BhaXIgKnRtcCA9IG1hbGxvYyhuKnNpemVvZihzdHJ1Y3QgcmFua19wYWlyKSk7Cglmb3IgKGludCBpID0gMCA7IGkgIT0gbiA7IGkrKykgewoJCXRtcFtpXS52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