Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1d6751

Browse files
Added Code
1 parent fa41b6f commit c1d6751

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef ALGO_RADIX_SORT_H__
2+
#define ALGO_RADIX_SORT_H__
3+
4+
#include <stdint.h>
5+
#include <string.h>
6+
#include <stdlib.h>
7+
#include <assert.h>
8+
#include <generic.h>
9+
#include <memory>
10+
11+
namespace alg {
12+
/**
13+
* couting sort
14+
*/
15+
static void radix_(int byte, const unsigned N, const uint32_t *source, uint32_t *dest) {
16+
unsigned count[256];
17+
unsigned index[256];
18+
memset(count, 0, sizeof (count));
19+
20+
unsigned i;
21+
for(i=0; i<N; ++i)
22+
count[((source[i])>>(byte*8))&0xff]++;
23+
24+
index[0]=0;
25+
for(i=1; i<256; ++i)
26+
index[i] = index[i-1] + count[i-1];
27+
28+
for(i=0; i<N; ++i)
29+
dest[index[((source[i])>>(byte*8))&0xff]++] = source[i];
30+
}
31+
32+
/**
33+
* radix sort a given unsigned 32-bit integer array of size N
34+
*/
35+
static void radix_sort(uint32_t *source, const unsigned N) {
36+
uint32_t * temp = new uint32_t[N];
37+
radix_(0, N, source, temp);
38+
radix_(1, N, temp, source);
39+
radix_(2, N, source, temp);
40+
radix_(3, N, temp, source);
41+
42+
delete [] temp;
43+
}
44+
45+
/**
46+
* check whether the array is in order
47+
*/
48+
static void check_order(const uint32_t *data, unsigned N) {
49+
for(--N ; N > 0; --N, ++data)
50+
assert(data[0] <= data[1]);
51+
}
52+
}
53+
54+
#endif //
55+
56+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /