DB Error: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

SELECT * FROM `user` the_table WHERE the_table.userid = DB Error: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

SELECT * FROM `user` the_table WHERE the_table.userid = C++ - Quicksort Algorithm for Strings Help? - iTechForums
Login
Search   Moderators Wanted: If you're interested please send your portfolio to info[at]itechforums.com
Welcome to the iTechForums.
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
To register now click here.
Latest Threads
Advertisements
Forum Statistics
Threads:
Posts: 5
Members:
Number of Users Online:
Welcome to our newest member,
Reply
 
Thread Tools Display Modes
  #1  
Old 02-18-2010, 07:04 PM
David David is offline
Member
 
Join Date: May 2008
Posts: 97
Default

C++ - Quicksort Algorithm for Strings Help?


C++ - Quicksort Algorithm for Strings Help?

I'm writing a Quicksort algorithm that sorts an array of strings. However, I am also under the requirement that is sorts this array based on a certain set of keys, stored in another array. For example, if I had the strings:

bear, cat, yellow

I might be requested to sort them by only the 2nd and 3rd characters, so the array of keys contains:

ea, at, el

I've got the algorithm working, but for some reason, it is very slow. I was just wondering if anyone could figure out where I'm going wrong. Keep in might that list is the array of strings, keylist is the matching array of keys, left is the left index to consider while partitioning, and right is the right index in this case. Here's the code (I know the formatting is going to get messed up, try to bear with it):

void quickSort(string *keylist, string *list, int left, int right){
if(left = right){
return;
}
int lhs = left;
int rhs = right - 1;
while(lhs rhs){
while(lhs = rhs keylist[lhs] = keylist[right]){
lhs++;
}
while(rhs = lhs keylist[rhs] = keylist[right]){
rhs--;
}
if(lhs rhs){
swap(keylist[lhs], keylist[rhs]);
swap(list[lhs], list[rhs]);
}
}
swap(keylist[lhs], keylist[right]);
swap(list[lhs], list[right]);
if(lhs 0){
quickSort(keylist, list, 0, lhs-1);
}
if(lhs right - 1){
quickSort(keylist, list, lhs + 1, right);
}
}

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2  
Old 02-18-2010, 07:04 PM
Chris C Chris C is offline
Member
 
Join Date: May 2008
Posts: 58
Default

Well, as a suggestion if this isn't for class only.
Why don't you simply use the existing qsort() function?
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

Define 2 different compare() functions.
Kind of like this:
int compareKeys (const void * a, const void * b);
int compareStrings (const void * a, const void * b);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 02-18-2010, 07:05 PM
juliepelletier juliepelletier is offline
Junior Member
 
Join Date: Jan 2010
Posts: 12
Default

First of all, your need to use the two lists doubles the work. I would try creating a struct for each record in list to include an id which could link back the string to keylist.

Of course you need to make sure you only swap pointers, not the contents, as this would be extremely heavy.

You could also use the built-in qsort which already takes a custom comparison function. You would basically just need to provide that function access to the keylist, either as a global or static (at the file level) variable.

Otherwise the code looks OK.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 02-18-2010, 07:05 PM
Pete S Pete S is offline
Junior Member
 
Join Date: Feb 2010
Posts: 9
Default

Code looks fine to me, how did you implement swap?

Are you swapping pointers or values in swap? Excessive copying in swap may be your speed problem.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 02-18-2010, 07:07 PM
MichaelInScarborough MichaelInScarborough is offline
Junior Member
 
Join Date: Oct 2009
Posts: 9
Default

I agree with Cris. You could use the qsort function in order to determine, whether your algorithm is comparibly slow. If it is, then you can start analyzing, where things go south!
this could be your comparison function

int compare(const void *a, const void *b)
{
string *strA = (string *)a, *strB = (string *)b;
return strA-compare(*strB);
}

In case you hate pointers you could try something like this:
int compare(const void *a, const void *b)
{
string strA = *(string *)a, strB = *(string *)b;
return strA.compare(strB);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quicksort Lomuto's algorithm? Elf Huang Software Design & Algorithms 1 01-22-2010 10:52 AM
Help Quicksort algorithm? honkeetonk Software Design & Algorithms 1 04-07-2009 05:20 PM
which is the best sorting algorithm??is it the heapsort or quicksort??plz expalin... Monal R Software Design & Algorithms 0 02-23-2009 05:37 PM
Why is the Quicksort is a popular sorting algorithm ? arkangels75 Software Design & Algorithms 0 06-04-2008 10:07 AM
Quicksort Algorithm Program in C++ Help? mcar_2185 Software Design & Algorithms 0 05-06-2008 10:15 AM

Your Ad Here

All times are GMT. The time now is 03:30 AM.