void lsd_sort(StringElementsArray * a, int N, int W, int R) {
int i,r,d,c;
int * count = newIntArray(R + 1);
StringElementsArray aux; // aux Array
createStringElementsArray(&aux,N); // create aux array
for (d = W-1; d >= 0; d--) {
// reset count[] array
for (i = 0; i < R+1; i++)
count[i]=0;
// compute frequency counts
for (i = 0; i < N; i++)
count[a->str[i][d] + 1]++;
// transform counts to indicies
for (r = 0; r < R; r++)
count[r+1] += count[r];
// distribute
for (i = 0; i < N; i++) {
c = a->str[i][d];
aux.str[count[c]] = a->str[i];
aux.len[count[c]] = a->len[i];
count[c]++;
}
// copy back
for (i = 0; i < N; i++) {
a->str[i] = aux.str[i];
a->len[i] = aux.len[i];
}
// Trace of iterations for LSD string sort
printf("\nLSD Sorted List (right to left) after sorting key %d:\n",d);
for (i = 0; i < N; i++)
printf("\t%s\n",a->str[i]);
}
Key- indexed countig
void key_index_counting(StringKeyElementsArray * a, int N, int R) {
int i,r,c;
int * count = newIntArray(R + 1);
// reset count[] array
for (i = 0; i < R + 1; i++)
count[i]=0;
StringKeyElementsArray aux; // aux Array
createStringKeyElementsArray(&aux,N); // create aux array
// compute frequency counts
for (i = 0; i < N; i++)
count[a->keys[i]]++;
// transform counts to indicies
for (r = 0; r < R; r++)
count[r+1] += count[r];//frequencias acumuladas
// distribute
for (i = 0; i < N; i++) {
c = a->keys[i] - 1;
aux.str[count[c]] = a->str[i];
aux.len[count[c]] = a->len[i];
aux.keys[count[c]] = a->keys[i];
count[c]++;
}
// copy back
for (i = 0; i < N; i++) {//4º passo
a->str[i] = aux.str[i];
a->len[i] = aux.len[i];
a->keys[i] = aux.keys[i];
}