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 62a9211

Browse files
Merge pull request #161 from D3v3sh5ingh/D3v3sh5ingh-patch-3
Radix sorting
2 parents 0561867 + 7993f81 commit 62a9211

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

‎Competitive Coding/Sorting/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ __Properties__
7575
* Best case performance O(n log n)
7676
* Average case performance depends on gap sequence
7777

78+
####Radix Sorting
79+
From [Wikipedia](https://en.wikipedia.org/wiki/Radix_sort): radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. Radix sort dates back as far as 1887 to the work of Herman Hollerith on tabulating machines.
80+
81+
__Properties__
82+
* Worst case performance O(wn)
83+
* Best case performance O(w + N)
84+
* Average case performance depends on gap sequence
85+
7886
###### View the algorithm in [action](https://www.toptal.com/developers/sorting-algorithms/shell-sort)
7987

8088
### Time-Compexity Graphs
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 によって変換されたページ (->オリジナル) /