Reputation: 51
first, qsort.c, then main.c. THEY ARE ON DIFFERENT FILES.
void qsort(int array[], int start, int end){
int temp_start = start, temp_end = end;
int part = array[start];
if(start >= end){
return;
}
while(temp_end > temp_start){
for(temp_end; array[temp_end] >= array[temp_start] && temp_end > temp_start; temp_end --);
array[temp_start] = array[temp_end];
for(temp_start; array[temp_start] < part && temp_end > temp_start; temp_start++);
array[temp_end] = array[temp_start];
}
array[temp_start] = part;
qsort(array, start, temp_start);
qsort(array, temp_start, end);
}
=================================================================================
#define MAX 7
#define START_POS 0
#define END_POS (MAX - 1)
void qsort(int array[], int start, int end);
void show(int array[], int size);
void random_input(int array[], int size);
int main(){
int array[MAX];
random_input(array, MAX);
show(array, MAX);
qsort(array, START_POS, END_POS);
show(array, MAX);
return 0;
}
I pass the array to qsort, but apparently I'm not able to modify its contents. Using the debugger I find that whenever a variable change on the array is issued, no change is shown on the debugger.
I have no idea of what is happening, if I let it go for a while the program ends up being stopped by windows.
I'm new to arrays and recursion, please help I'm really at loss here.
Upvotes: 0
Views: 57
Reputation: 51
I solved it, the call wasn't right making this kind of situation possible.
7 8
temp_start ^
temp_end....^
and then calls were:
qsort(array, start, temp_start)
//start and temp_start are equal, after call qsort returns
7
temp_start ^
temp_end....^
//and the second call
qsort(array, temp_start, end)
7 8
temp_start ^
end .........................^
so the second qsort call will never return, it will end on the scenario from the beginning again:
//first call
7
temp_start ^
temp_end....^
//second call
7 8
temp_start ^
temp_end.....................^
A change of the second qsort call to:
qsort(array, temp_start + 1, end);
solves the problem and gives correct program output.
Upvotes: 1