In Programming Pearls there is an algorithm that sorts varying length arrays but sorts in time proportional to the sum of their length. For example, if we have a record array x[0...n-1]
, and each record has an integer length and a pointer to array bit[0...length-1]
.
The code is implemented this way:
void bsort(l, u, depth){
if (l >= u)
return ;
for (i = l; i <= u; i++){
if (x[i].length < depth)
swap(i, l++);
}
m = l;
for (int i = l; i < u; i++){
if (x[i].bit[depth] == 0)
swap(i, m++);
}
bsort(l, m - 1, depth + 1);
bsort(m, u, depth + 1);
}
My question is that, given the record:
x[6] = {"car", "bus", "snow", "earth", "dog", "mouse"}
I know how to get the string length, but what about with a bit array? How could I make a bit array suitable for this string array? And even x[i].bit[depth]
how can I implement this?