Hi, I have copied the quicksort algorithm out of my text book, but it isn't working properly and there are no compiler errors so I am struggling to debug. it is partially sorting so I think it must be one of the conditional statements or something not declared how I expect but everything looks okay.
Also I would prefer to use a string rather than a character array in the main program but I can't convert the two since c_str() gives a const so doesn't work and I've tried a few other things.
This is the text book I am using pg 38 <a href "http://download.microsoft.com/download/7/3/7/7374dd3f-be0e-4fc1-ac9b-e3d652194687/C++%20Beginner's%20Guide%20CH05.xps" > link </a>
Sorry I don't like posting chunks of code but I just can't spot the problem.
[spoiler]
char alphabet[]={'s','d','l', 'k','w','o','a','e','x','x','z','y','\0'};
string name;
name = "Brian";
for(int i=0; alphabet[i]; i++)
{
cout << alphabet[i] ;
}
cout <<endl <<endl;
quicksort(alphabet,strlen(alphabet));
// quicksort(name[0],name.length());
for(int i=0; i<strlen(alphabet); i++)
{
cout << alphabet[i];
}
//cout << name;
cin.ignore();
cin.get();
return 0;
}
void qs(char *items, int left, int right)
{
int i,j;
char x,y;
i=left;
j=right;
x=items[(left+right)/2];
do
{
while(items[i]<x && (i<right)) i++;
while((x<items[j]) && (j<left)) j--;
if(i<=j)
{
y=items[i];
items[i]=items[j];
items[j]=y;
i++; j--;
}
}while(i<=j);
if(left < j) qs(items, left, j);
if(right > i) qs(items, i, right);
}
[/spoiler]
EDIT: My attempt to use spoiler tages and hyperlink massively fail lol. I did preview but thought it just musn't work in the preview window