Пример #1
0
static void rad_sort_u(uint *from, uint *to, uint bit) {
	if(!bit || to < from + 1) return;
	uint *ll = from, *rr = to - 1, tmp;
	while(1) {
		while(ll < rr && !(*ll & bit)) ll++;
		while(ll < rr && (*rr & bit)) rr--;
		if(ll >= rr) break;
		swap(*ll, *rr);
	}
	if(!(bit & *ll) && ll < to) ll++;
	bit >>= 1;
	rad_sort_u(from, ll, bit);
	rad_sort_u(ll, to, bit);
}
Пример #2
0
/* sort signed ints: flip highest bit, sort as unsigned, flip back */
static void radix_sort(int *a, const size_t len) {
  size_t i;
  uint *x = (uint*) a;

  each(i, len) x[i] ^= INT_MIN;
  rad_sort_u(x, x + len, INT_MIN);
  each(i, len) x[i] ^= INT_MIN;
}
Пример #3
0
/* sort unsigned ints */
static void rad_sort_u(uint *from, uint *to, uint bit) {
  uint *ll = from, *rr = to - 1, tmp;

  if (!bit || to < from + 1) {
    return;
  }
  while (1) {
    /* find left most with bit, and right most without bit, swap */
      while(ll < rr && !(*ll & bit)) ll++;
      while(ll < rr && (*rr & bit)) rr--;
      if (ll >= rr) break;
      swap(*ll, *rr);
  }

  if (!(bit & *ll) && ll < to) ll++;
  bit >>= 1;

  rad_sort_u(from, ll, bit);
  rad_sort_u(ll, to, bit);
}
Пример #4
0
void radix_sort(int *a, const size_t len)
{
	size_t i;
	uint *x = (uint*) a;
	// Flip negative signs so sorting works properly
	each(i, len) x[i] ^= INT_MIN;
	// Sort unsigned
	rad_sort_u(x, x+len, INT_MIN);
	// Flip negative signs back 
	each(i, len) x[i] ^= INT_MIN;
}
Пример #5
0
static inline void radix_sort_unsigned(uint *a, const size_t len) {
  rad_sort_u(a, a + len, (uint) INT_MIN);
}