1

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?

Faraz
  • 189
  • 3
  • 9
  • I tried to clean this up but your question still isn't very clear. Is it basically that you're thinking of sorting a `char[]` and you want to know if you can sort a bit array? I think it depends on how your bit array is implemented. – Brendan Long Oct 15 '11 at 05:32
  • i want to say that,it is not clear to me too how can i implement bit sort with string's record,can't understand it –  Oct 15 '11 at 05:42

1 Answers1

1

Arrays of chars (or any other type, for that matter) are also arrays of bits - chars are made of bits, after all. So you don't have to create a separate array, you just have to find a way to access a given bit in the array. For that, you'll have to use some bit manipulations. You can find a few examples of how this could be done here: Any smarter way to extract from array of bits?.

Basically, you first have to figure out the byte the required bit is at, and then get that specific bit's value. Something along:

char* array = "the array";
int required_bit = 13;
int bit = required_bit & 0x7;  // get the bit's offset in its byte
int byte = required_bit >> 3;  // get the bit's byte
int val = (array[byte] >> bit) & 0x1; // check if the bit is 1

Now wrap this in a function (possibly with additional bound checks, to make sure the given required_bit is not outside of the array), and use with x[i].

Community
  • 1
  • 1
Eran
  • 21,632
  • 6
  • 56
  • 89
  • it is very great answer @eran,just one question suppose instead of string array,we have integer array,then x[i].length it is number of digit for represent this number yes?and additionaly x[i].bit[depth] it is bit in number at position depth yes? –  Oct 15 '11 at 06:02
  • You can't use `x[i].length` to get an array's length, this is not Java... With strings, you can use `strlen`. With ints, you have to pass the size along with the array. Also, you don't have a `bit[depth]` part - you work on `x[i]`. Lastly, if you have an int array, you'll have to make some changes - ints aren't 1 byte, so `array[byte]` won't access the `byte` byte, but the `byte` int. easiest would be to cast the `int*` into a `char*`. – Eran Oct 15 '11 at 06:11
  • sorry @eran,but i am confused already,in my example how my code would be written?i have string array shown in example,please tell me how would my code looks? –  Oct 15 '11 at 06:23
  • You should first, say, create a `int get_bit(char* array, int bitpos)` function. Then, call it as `int bit_val = get_bit(x[i], depth)` (assuming `depth` is the bit whose value you're interested in). This will get you the `depth` bit in the `x[i]` array. – Eran Oct 15 '11 at 06:29
  • but x[i] is array of characters not one character yes?or it does not matter –  Oct 15 '11 at 06:34
  • It should be an array of chars. Look at the function's definition and the example code. – Eran Oct 15 '11 at 06:39
  • ok @eran thank you for help,i will try to understand it,just only one confusion is that,even i will return my example,we should take x array,which contains strings and these strings itself are array of chars,so what a hell code from programming pearls –  Oct 15 '11 at 06:46