S
Shalini
Hi ,
Is the code below efficient??
bool *isElement(char **a1,long lengtha1,char **a2,long lengtha2){
charQuickSort(a2, 0, (lengtha2-1));
bool *tag=(bool*)calloc(lengtha1,sizeof(bool));
int i,j;
for(j = 0; j < lengtha1; j++){
for(i = 0; i < lengtha2;i++){
if(strcmp(a1[j],a2)==0){
tag[j]=true;
// printf("%d\n",tag[j]);
break;
}
if(strcmp(a1[j],a2)<0 || (i == (lengtha2-1))){
tag[j] = false;
// printf("%d\n",tag[j]);
break;
}
}
}
return(tag);
}
void charQuickSort(char **vals, int l, int r)
{
int i,j,m;
char **v=(char **)calloc(1,sizeof(char *));
char **t=(char **)calloc(1,sizeof(char *));
if(r > l)
{
m = (r+l)/2;
if(strcmp(vals[m] ,vals[r])<0)
{
if(strcmp(vals[l] ,vals[m])<0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[m];
vals[m] = t[0];
//vals[m] = strdup(t);
}
else if (strcmp(vals[l] ,vals[r])<0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[l];
vals[l] = t[0];
//vals[l] = strdup(t);
}
}
else
{
if(strcmp(vals[l] ,vals[m])>0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[m];
vals[m] = t[0];
//vals[m] = strdup(t);
}
else if (strcmp(vals[l], vals[r])>0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[l];
vals[l] = t[0];
//vals[l] = strdup(t);
}
}
v[0]=vals[r];
// strcpy(v,vals[r]);
i = l-1;
j = r;
do {
for(i++; strcmp(vals, v[0])<0 && i <= r; i++) {
// printf("%d ",i);
}
/*for(i++; strcmp(vals, v)<0 && i <= r; i++) {
//printf("%d ",i);
}*/
for(j--; strcmp(vals[j],v[0])>0 && j > l; j--);
// for(j--; strcmp(vals[j],v)>0 && j > l; j--);
t[0]=vals;
// strcpy(t,vals);
vals = vals[j];
vals[j] = t[0];
//vals[j] = strdup(t);
}
while( j > i);
vals[j] = vals;
vals = vals[r];
vals[r] = t[0];
//vals[r] = strdup(t);
charQuickSort(vals, l, i-1);
charQuickSort(vals,i+1,r);
}
}
Is the code below efficient??
bool *isElement(char **a1,long lengtha1,char **a2,long lengtha2){
charQuickSort(a2, 0, (lengtha2-1));
bool *tag=(bool*)calloc(lengtha1,sizeof(bool));
int i,j;
for(j = 0; j < lengtha1; j++){
for(i = 0; i < lengtha2;i++){
if(strcmp(a1[j],a2)==0){
tag[j]=true;
// printf("%d\n",tag[j]);
break;
}
if(strcmp(a1[j],a2)<0 || (i == (lengtha2-1))){
tag[j] = false;
// printf("%d\n",tag[j]);
break;
}
}
}
return(tag);
}
void charQuickSort(char **vals, int l, int r)
{
int i,j,m;
char **v=(char **)calloc(1,sizeof(char *));
char **t=(char **)calloc(1,sizeof(char *));
if(r > l)
{
m = (r+l)/2;
if(strcmp(vals[m] ,vals[r])<0)
{
if(strcmp(vals[l] ,vals[m])<0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[m];
vals[m] = t[0];
//vals[m] = strdup(t);
}
else if (strcmp(vals[l] ,vals[r])<0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[l];
vals[l] = t[0];
//vals[l] = strdup(t);
}
}
else
{
if(strcmp(vals[l] ,vals[m])>0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[m];
vals[m] = t[0];
//vals[m] = strdup(t);
}
else if (strcmp(vals[l], vals[r])>0)
{
t[0]=vals[r];
//strcpy(t,vals[r]);
vals[r] = vals[l];
vals[l] = t[0];
//vals[l] = strdup(t);
}
}
v[0]=vals[r];
// strcpy(v,vals[r]);
i = l-1;
j = r;
do {
for(i++; strcmp(vals, v[0])<0 && i <= r; i++) {
// printf("%d ",i);
}
/*for(i++; strcmp(vals, v)<0 && i <= r; i++) {
//printf("%d ",i);
}*/
for(j--; strcmp(vals[j],v[0])>0 && j > l; j--);
// for(j--; strcmp(vals[j],v)>0 && j > l; j--);
t[0]=vals;
// strcpy(t,vals);
vals = vals[j];
vals[j] = t[0];
//vals[j] = strdup(t);
}
while( j > i);
vals[j] = vals;
vals = vals[r];
vals[r] = t[0];
//vals[r] = strdup(t);
charQuickSort(vals, l, i-1);
charQuickSort(vals,i+1,r);
}
}