counting sort
there is a restriction for counting sort,that is we must know the range of the elements;
we can count the number of each integer in range among array to be sorted,since we can create a array with length of the range as a temporary working storage;that's why we call it counting sort;
once we know the algorithm,it's easy to design a pseudocode as follow;
procedure counting-sort(A,lengthA,k) //A to be sorted,k range of integer;
for(i = 0; i < k; i++ ) {
__C[i] = 0;
}
for(i = 0; i < lengthA; i++ ) {
__C[A[i]]++;
}
for(i = 0, j = 0; i < k; i++ ) {
__while(C[i]--) A[j++] = i;
}
end procedure;
that's all;
we can count the number of each integer in range among array to be sorted,since we can create a array with length of the range as a temporary working storage;that's why we call it counting sort;
once we know the algorithm,it's easy to design a pseudocode as follow;
procedure counting-sort(A,lengthA,k) //A to be sorted,k range of integer;
for(i = 0; i < k; i++ ) {
__C[i] = 0;
}
for(i = 0; i < lengthA; i++ ) {
__C[A[i]]++;
}
for(i = 0, j = 0; i < k; i++ ) {
__while(C[i]--) A[j++] = i;
}
end procedure;
that's all;
0 Comments:
Post a Comment
<< Home