I managed to get a quicksort function to sort the numbers in a dynamically allocated array of structs,
struct Contact {
char first[maxstring];
char second[maxstring];
int number;
};
//and the dynamically allocated array,
Contact *person = new Contact[numberofcontacts];
//The quicksort function I used to sort numbers is:
void Quicksortnumber(Contact *person, int left, int right) {
int L = left;
int R = right;
int pivot = person[(left + right) / 2].number; //partition
while (L <= R) {
while (person[L].number < pivot){
L++;
}
while (person[R].number > pivot){
R--;
}
if (L <= R) {
swap(person[L], person[R]);
L++;
R--;
}
} //recursion to complete sort
if (left < R){
Quicksortnumber(person, left, R);
}
if (L < right){
Quicksortnumber(person, L, right);
}
}
/*This works fine for the integers I then tried to adapt this to work for the "first" member of the structure*/
void Quicksortfirst(Contact *person, int left, int right) {
int L = left;
int R = right;
char pivot[maxstring];
strcpy(pivot, person[(left+right)/2].first);
while(L <= R) {
int diff1 = strcomp(person[L].first, pivot);
while (diff1<0){
L++;
} //scrolls from left to right
int diff2 = strcomp(person[R].first,pivot);
while (diff2>0){
R--; //scrolls from right to left
}
if (L <= R) {
swap(person[L], person[R]);
L++;
R--;
} //swaps structs
} //recursion to complete sort
if (left < R){
Quicksortfirst(person, left, R);
} //repeats for lower part of split array
if (L < right){
Quicksortfirst(person, L, right);
} //repeats for higher part of split array
}
When I debug this, no errors are produced but when I build the code and select it to sort by first name the compiler seems to stall. I cant understand why this wouldn't work and without an error message I cant guess where it's going wrong.
Thanks for any help.