所以长话短说,我正在尝试为向量实现计数排序。我的代码中某处有一些错误,尽管我认为我已经非常严格地遵循了伪代码。
功能:
void csort(vector<int>& a, vector<int>& b)
{
vector<int> c(a.size());
for(int i = 0; i <= c.size(); i++)
c[i] = 0;
for(int i = 0; i <= a.size(); i++ )
{
c[a[i]]++;
}
for(int i = 0; i <= c.size(); i++)
{
c[i]+= c[i-1];
}
for(int i = a.size(); i < 1; i--)
{
b[c[a[i]]] = a[i];
c[a[i]] =c[a[i]] -1;
}
}
伪代码:
let C[k] be a new array
for i = 0 to k
C[i] = 0
for j = 1 to A:length
C[A[j]] = C[A[j]] +1
for i = 1 to k
C[i] = C[i]+ C[i-1]
for j = A:length downto 1
B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]] -1