Created
August 25, 2023 17:08
-
-
Save kspalaiologos/7f0ba21f5532e4079e54084ddb5479a0 to your computer and use it in GitHub Desktop.
practical o(n) {⍵[⍋⍵]} if {645=⎕dr⍵}
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
void magic(float * x, size_t n) { | |
uint32_t * result = malloc(n * sizeof(uint32_t)), | |
* buckets = malloc(0x100*sizeof(uint32_t)), | |
* si = malloc(0x100*sizeof(uint32_t)), | |
* y = (uint32_t *) x, key, temp; | |
for (size_t i = 0; i < n; i++) y[i] ^= 0x80000000; | |
for (int i = 0; i < n; ++i) ++buckets[y[i] & 0xFF]; | |
si[0] = 0; | |
for (int j = 1; j < 0x100; ++j) si[j] = si[j - 1] + buckets[j - 1]; | |
for (int i = n-1; i >= 0; --i) key = y[i] & 0xFF, result[si[key] + --buckets[key]] = y[i]; | |
memcpy(y,result,n*sizeof(uint32_t)); | |
for (int i = 0; i < n; ++i) ++buckets[(y[i] & 0xFF00) >> 8]; | |
si[0] = 0; | |
for (int j = 1; j < 0x100; ++j) si[j] = si[j - 1] + buckets[j - 1]; | |
for (int i = n-1; i >= 0; --i) key = (y[i] & 0xFF00) >> 8, result[si[key] + --buckets[key]] = y[i]; | |
memcpy(y,result,n*sizeof(uint32_t)); | |
for (int i = 0; i < n; ++i) ++buckets[(y[i] & 0xFF0000) >> 16]; | |
si[0] = 0; | |
for (int j = 1; j < 0x100; ++j) si[j] = si[j - 1] + buckets[j - 1]; | |
for (int i = n-1; i >= 0; --i) key = (y[i] & 0xFF0000) >> 16, result[si[key] + --buckets[key]] = y[i]; | |
memcpy(y,result,n*sizeof(uint32_t)); | |
for (int i = 0; i < n; ++i) ++buckets[(y[i] & 0xFF000000) >> 24]; | |
si[0] = 0; | |
for (int j = 1; j < 0x100; ++j) si[j] = si[j - 1] + buckets[j - 1]; | |
for (int i = n-1; i >= 0; --i) key = (y[i] & 0xFF000000) >> 24, result[si[key] + --buckets[key]] = y[i]; | |
memcpy(y,result,n*sizeof(uint32_t)); | |
free(result); | |
free(buckets); | |
free(si); | |
for (size_t i = 0; i < n; i++) y[i] ^= 0x80000000; | |
key = 0; | |
for (size_t i = 0; i < n; i++) if (y[i] & 0x80000000) key++; | |
for (size_t i = 0; i < key / 2; i++) temp = y[i], y[i] = y[key - i - 1], y[key - i - 1] = temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment