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[i])==0){
tag[j]=true;
// printf("%d\n",tag[j]);
break;
}
if(strcmp(a1[j],a2[i])<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[i], v[0])<0 && i <= r; i++) {
// printf("%d ",i);
}
/*for(i++; strcmp(vals[i], 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[i];
// strcpy(t,vals[i]);
vals[i] = vals[j];
vals[j] = t[0];
//vals[j] = strdup(t);
}
while( j > i);
vals[j] = vals[i];
vals[i] = vals[r];
vals[r] = t[0];
//vals[r] = strdup(t);
charQuickSort(vals, l, i-1);
charQuickSort(vals,i+1,r);
}
} 2 1547
Shalini wrote: 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[i])==0){ tag[j]=true; // printf("%d\n",tag[j]); break; } if(strcmp(a1[j],a2[i])<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[i], v[0])<0 && i <= r; i++) { // printf("%d ",i); } /*for(i++; strcmp(vals[i], 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[i]; // strcpy(t,vals[i]); vals[i] = vals[j]; vals[j] = t[0]; //vals[j] = strdup(t);
} while( j > i); vals[j] = vals[i]; vals[i] = vals[r]; vals[r] = t[0]; //vals[r] = strdup(t); charQuickSort(vals, l, i-1); charQuickSort(vals,i+1,r); }
}
Here is pseudocode for a very similar problem I needed to solve... I
solved it in O(nln), where your solution is O(n^2).
Problem: Given Arrays A1 and A2, find if there exist any in common
Solution:
1. Sort A1 with an O(nln) search. *hint... QuickSort is O(n^2) in worst
case, so it is not really the best, if you have a lot of numbers.... You
can do better, usually
2. For every item in A2 (O(n)), do a binary search for the item in A1
(O(ln), since it is sorted).
Each step is O(nln), which gives an O(nln) solution.
That is the fastest that I know how to solve the problem.
Brian
Brian Genisio wrote: Shalini wrote:
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[i])==0){ tag[j]=true; // printf("%d\n",tag[j]); break; } if(strcmp(a1[j],a2[i])<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[i], v[0])<0 && i <= r; i++) { // printf("%d ",i); } /*for(i++; strcmp(vals[i], 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[i]; // strcpy(t,vals[i]); vals[i] = vals[j]; vals[j] = t[0]; //vals[j] = strdup(t);
} while( j > i); vals[j] = vals[i]; vals[i] = vals[r]; vals[r] = t[0]; //vals[r] = strdup(t); charQuickSort(vals, l, i-1); charQuickSort(vals,i+1,r); } } Here is pseudocode for a very similar problem I needed to solve... I solved it in O(nln), where your solution is O(n^2).
Problem: Given Arrays A1 and A2, find if there exist any in common Solution:
1. Sort A1 with an O(nln) search. *hint... QuickSort is O(n^2) in worst case, so it is not really the best, if you have a lot of numbers.... You can do better, usually
2. For every item in A2 (O(n)), do a binary search for the item in A1 (O(ln), since it is sorted).
Each step is O(nln), which gives an O(nln) solution.
That is the fastest that I know how to solve the problem. Brian
After looking again, I did not give you a solution for the problem you
put out... sorry.
To clarify... quicksort cannot guarantee efficiency, so I would use
something else.... possibly a randomized quicksort, which is better for
the average time, but is still worst case O(n^2)
For your problem, I have a better solution (for speed efficiency)...
Sort BOTH arrays using an O(nln) sort.
Move pointers in both arrays, since they are both sorted... Using
integers, you can see, you can compare both in a _single scan. The same
is true for string compares.
1 3 4 6 7 10
1 2 5 6 7 9 10
This solution will solve your problem in O(nln), which beats your
current solution of O(n^2)
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Psybar Phreak |
last post by:
hi all
i have an array of Process objects, each elements has an id (process.id),
and arrival time (process.at), processing time (process.pt) and a boolean
indicating whether the process has...
|
by: Madestro |
last post by:
Hi guys,
I am making a small program to retrieve e-mails from POP accounts. I got all
the e-mail parsing stuff figured out, but I cannot seem to come up with a way
to find out which e-mails are...
|
by: Massimiliano Alberti |
last post by:
Can someone check this? If it's OK, you can use however you want... :-)
It should search for an element in an array and, if it can't find it, return
the next element.
key is what you are...
|
by: Busin |
last post by:
bool arr;
arr = true;
For the element arr, is there any fastest algorithm to fast find the two
elements on both side of "n" and both elements are cloest to "23"?
Thanks!
|
by: Stacey Levine |
last post by:
I have a webservice that I wanted to return an ArrayList..Well the service
compiles and runs when I have the output defined as ArrayList, but the WSDL
defines the output as an Object so I was...
|
by: Charles Law |
last post by:
I have a complex object that I am serializing, and I would like to omit
elements that have a default value.
For example, if I have a class as follows
Public Class Test
Private m_Name As...
|
by: Tuxedo |
last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any
elements thereafter in an array in random order:
var a = new...
|
by: C C++ C++ |
last post by:
Hi all, got this interview question please respond.
How can you quickly find the number of elements stored in a a) static
array b) dynamic array ?
Rgrds
MA
|
by: ALKASER266 |
last post by:
Hey guyz
I have a prac and I am beginner and I did this code>
Is my code is complete and if is it not complete how i can complete it?
and how i can arrange it more?
How I can make my driver to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |