Skip to content

Instantly share code, notes, and snippets.

@sharat
Created October 19, 2012 06:34
Show Gist options
  • Save sharat/3916556 to your computer and use it in GitHub Desktop.
Save sharat/3916556 to your computer and use it in GitHub Desktop.
radi sort
void radix_sort(unsigned *begin, unsigned *end)
{
unsigned *begin1 = new unsigned[end - begin];
unsigned *end1 = begin1 + (end - begin);
for (unsigned shift = 0; shift < 32; shift += 8) {
size_t count[0x100] = {};
for (unsigned *p = begin; p != end; p++)
count[(*p >> shift) & 0xFF]++;
unsigned *bucket[0x100], *q = begin1;
for (int i = 0; i < 0x100; q += count[i++])
bucket[i] = q;
for (unsigned *p = begin; p != end; p++)
*bucket[(*p >> shift) & 0xFF]++ = *p;
std::swap(begin, begin1);
std::swap(end, end1);
}
delete[] begin1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment