Click here to Skip to main content
15,885,707 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
void quickSortMain(char items[][10], int count);
void quickSort(char items[][10], int left, int right);
int main(void)
{
  int i;
  char str[][10] = { "this","is","a","test","ustafa","x","q","mu","a","f","u","t","d","g","g","c"};
  quickSortMain(str,16);
  for(i=0; i<16; i++) {
     printf("%s\n", str[i]);
  }
  getch();
  return 0;
}
void quickSortMain(char items[][10], int count)
{
  quickSort(items, 0, count-1);
}
void quickSort(char items[][10], int left, int right)
{
  int i, j;
  char *x;
  char temp[10];
  i = left;
  j = right;
  x = items[(left+right)/2];
  do {
    while((strcmp(items[i],x) < 0) && (i < right)) {
       i++;
    }
    while((strcmp(items[j],x) > 0) && (j > left)) {
        j--;
    }
    if(i <= j) {
      strcpy(temp, items[i]);
      strcpy(items[i], items[j]);
      strcpy(items[j], temp);
      i++;
      j--;
   }
  } while(i <= j);
  if(left < j) {
     quickSort(items, left, j);
  }
  if(i < right) {
     quickSort(items, i, right);
  }
}
Posted
Updated 18-Jan-11 19:10pm
v2
Comments
JF2015 19-Jan-11 1:11am    
Edited to add code formatting.


What is your question? Does your code not work?
Rajesh Anuhya 19-Jan-11 1:11am    
what happens??, what is your problem??
Sandeep Mewara 19-Jan-11 1:16am    
Issue?
Kasson 19-Jan-11 1:23am    
What is your problem?
Aescleal 19-Jan-11 11:01am    
If this was meant to be an answer to a question it's customary to post it as an answer to the question so the original poster can see it. If it's not would Sir/Madam care to enlighten us?

1 solution

Not sure what your problem is, but a few pointers:
1. what is wrong with qsort() from stdlib.h?
2. you are using strcpy to copy the items, it would be much more preferable to just copy the pointes to the strings (this may also be the source of any problems) since you will be over-running some string buffers:

char str[][10] = { "this","is","a","test","ustafa","x","q","mu","a","f","u","t","d","g","g","c"};


For the string "a" for example, 2 bytes is allocated on the stack for it {'a', '\0'}, if you then copy "this" into that location, you will overrun the buffer (copying 5 bytes), most likely into the next string, "test".

This is most likely the cause if what you are getting is a big jumble of random characters
 
Share this answer
 
Comments
Andrew Brock 19-Jan-11 1:40am    
I recently posted this: http://www.codeproject.com/Messages/3739218/Re-case-sensitive-sort-of-chars-strings-including-.aspx with pretty much what you are trying to do implemented using the qsort() function
Aescleal 19-Jan-11 10:50am    
The original poster declared a 2 dimensional array of characters, each row of which is 10 characters long so your point 2 is inaccurate - there's more than enough room to hold all the strings he's using.
If he'd specified the array as a array of pointers to characters then they'd be opening themselves up to stack corruption problems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900