28
loading...
This website collects cookies to deliver better user experience
void CountingSort(vector<int>& keys) {
int max_key = *max_element(keys.begin(), keys.end());
vector<int> bookkeeping(max_key + 1);
for (int key : keys) bookkeeping[key]++;
for (int i = 0, k = 0; i <= max_key; ++i) {
for (int j = 0; j < bookkeeping[i]; ++j) {
keys[k++] = i;
}
}
}
the output position of the first element with key i corresponds to the number of elements with key < i
the bookkeeping subarray with indices in the range [0,i-1] contains the frequencies of all the elements with key < i
vector<pair<int, string>> CountingSort(vector<pair<int, string>>& items) {
int max_key = max_element(items.begin(), items.end(),
[](auto const& x, auto const& y) {
return x.first < y.first;
})->first;
vector<int> bookkeeping(max_key+1, 0);
//counter[i] corresponds to the number of entries with key equal to i
for (const auto& item : items) {
bookkeeping[item.first]++;
}
//nextIndex[i] corresponds to the number of entries with key less than i
vector<int> next_index(max_key+1, 0);
for (int i = 1; i < next_index.size(); ++i) {
next_index[i] = next_index[i-1] + bookkeeping[i-1];
}
vector<pair<int, string>> output(items.size());
for (const auto& item : items) output[next_index[item.first]++] = item;
return output;
}
vector<pair<int, string>> CountingSort(vector<pair<int, string>>& items) {
int max_key = max_element(items.begin(), items.end(),
[](auto const& x, auto const& y) {
return x.first < y.first;
})->first;
vector<int> bookkeeping(max_key + 1, 0);
//count keys frequency
for (const auto& item : items) {
bookkeeping[item.first]++;
}
//at the end each element is the index of the last element with that key
std::partial_sum(bookkeeping.begin(), bookkeeping.end(), bookkeeping.begin());
vector<pair<int, string>> output(items.size());
//build sorted output iterating backward
for (auto it = items.crbegin(); it != items.crend(); ++it) {
output[--bookkeeping[it->first]] = *it;
}
return output;
}
time complexity is O(n+k) to iterate through both the input array and the bookkeeping array
space complexity is O(k) to store the bookkeeping array.
def countingSort(input):
maxKey= max(input, key=lambda item:item[0])[0]
bookeepingLength = maxKey+1
bookeeping = [0] * bookeepingLength
# Count keys frequency
for item in input:
bookeeping[item[0]] += 1
# at the end each element is the index
# of the last element with that key
for i in range(1, bookeepingLength):
bookeeping[i] += bookeeping[i-1]
output = [0] * len(input)
# build sorted output iterating backward
i = len(input) - 1
while i >= 0:
item = input[i]
bookeeping[item[0]] -= 1
position = bookeeping[item[0]]
output[position] = item
i -= 1
return output
class CountingSort {
private static int[] PartialSum(int[] input)
{
for (int i = 1; i < input.Count(); i++)
{
input[i] = input[i] + input[i - 1];
}
return input;
}
public static (int, string)[] CountingSort((int, string)[] items)
{
int max_key = items.Max(t => t.Item1);
var bookkeeping = new int[max_key + 1];
//count keys frequency
foreach (var item in items) {
bookkeeping[item.Item1]++;
}
//at the end each element is the index of the last element with that key
bookkeeping = PartialSum(bookkeeping);
var output = new (int, string)[items.Length];
//build sorted output iterating backward
for (int i = items.Length - 1; i >= 0; i--) {
output[--bookkeeping[items[i].Item1]] = items[i];
}
return output;
}
}