0

This question is a follow-up to a question I asked earlier on Stack Overflow: bsort example from programming pearls

I want to show you my work and have some questions about it.

#include<string.h>
#include<iostream>
using namespace std;
void swap(char *x[],int i,int j)
{
    char *t=x[i];
    x[i]=x[j];
    x[j]=t;
}

int get_bit(char *s,int depth)
{
    int required_bit=13;
    int bit=required_bit&0x7;
    int byte=required_bit>>3;
    int val=(s[byte]>>bit)&0x1;
    return val;
}

void bsort(char *x[],int l,int u,int depth)
{
    if(l>=u) return ;
    for(int i=l;i<=u;i++){
        if(strlen(x[i])<depth){
            swap(x,i,l++);
        }
    }
    int m=l;
    for(int i=l;i<u;i++)
    {
        if(get_bit(x[i],depth)==0)
        {
            swap(x,i,m++);
        }
    }

    bsort(x,l,m-1,depth+1);
    bsort(x,m,u,depth+1);
}

int main()
{
    char *x[6]={"car","bus","snow","earth","dog","mouse"};
    bsort(x,0,5,1);
    for(int i=0;i<6;i++)
        cout<<x[i]<<"  "<<endl;

    return 0;
}

The code above works, compiles and shows me a result. But I am interested to know whether it sorts lexicographically or otherwise? The result is this:

car  
bus  
dog  
snow  
earth  
mouse

First b is before c, so are car and bus on the right place? Please tell me what is wrong with my code.

Community
  • 1
  • 1

0 Answers0