-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_sort.cpp
136 lines (115 loc) · 2.69 KB
/
radix_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <string>
/*
worst: O(w * n), where w is the number of bits required to store each key
Also known as bucket or digital sort
*/
/*
Radix sort an array of Strings
Assume all are all ASCII
Assume all have same length
*/
void RadixSortWithFixedLength(const int stringLen, std::vector<std::string>* const outData)
{
const int BUCKETS = 256;
std::vector<std::vector<std::string>> buckets(BUCKETS);
for(int pos = stringLen - 1; pos >= 0; --pos)
{
for(std::string& s : *outData)
{
buckets[s[pos]].push_back(std::move(s));
}
int index = 0;
for(auto bucket : buckets)
{
for(std::string& s: bucket)
{
outData->at(index++) = std::move(s);
}
bucket.clear();
}
}
}
/*
Counting radix sort an array of Strings
Assume all are all ASCII
Assume all have same length
*/
void CountingRadixSort(const int stringLen, std::vector<std::string>* const outData)
{
const int BUCKETS = 256;
int n = outData->size();
std::vector<std::string> buffer(n);
std::vector<std::string>* in = outData;
std::vector<std::string>* out = &buffer;
for(int pos = stringLen - 1; pos >= 0; --pos)
{
std::vector<int> count(BUCKETS + 1);
for(int i = 0; i < n; i++)
{
++count[in->at(i)[pos] + 1];
}
for(int b = 1; b <= BUCKETS; b++)
{
count[b] += count[b - 1];
}
for(int i = 0; i < n; i++)
{
out->at(count[in->at(i)[pos]]++) = std::move(in->at(i));
}
// swap in and out roles
std::swap(in, out);
}
if(stringLen % 2 == 1) // if odd number of passes, in is buffer, out is outData, so move back
{
for(int i = 0; i < outData->size(); i++)
{
outData->at(i) = std::move(in->at(i));
}
}
}
/*
Radix sort an array of Strings
Assume all are all ASCII
Assume all have length bounded by maxLen
*/
void RadixSortWithVariableLengths(const int maxLen, std::vector<std::string>* const outData)
{
const int BUCKETS = 256;
std::vector<std::vector<std::string>> wordsByLength(maxLen + 1);
std::vector<std::vector<std::string>> buckets(BUCKETS);
for(std::string& s : *outData)
{
wordsByLength[s.length()].push_back(std::move(s));
}
int index{ 0 };
for(auto& wordList : wordsByLength)
{
for(std::string& s : wordList)
{
outData->at(index++) = std::move(s);
}
}
int startingIndex = outData->size();
for(int pos = maxLen - 1; pos >= 0; pos--)
{
startingIndex -= wordsByLength[pos + 1].size();
for(int i = startingIndex; i < outData->size(); i++)
{
buckets[outData->at(i)[pos]].push_back(std::move(outData->at(i)));
}
index = startingIndex;
for(auto& bucket : buckets)
{
for(std::string& s : bucket)
{
outData->at(index++) = std::move(s);
}
bucket.clear();
}
}
}
int main()
{
}