I am trying to code a partition quicksort but the problem here is at the output is in an infinite loop:
the input : 4 3 5 7 2
the expected output : 2 3 4 5 7
but my output is infinite :
2 3 4 5 7
2 3 4 5 7
2 3 4 5 7
.
.
and so on
I would suggest using your debugger to check the values of j and i at each iteration and to check why the termination condition is not being reached.
Ofcourse, there is a possiblity that the error lies elsewhere but you can trace it through the debugger or just outputting some status messages to the console.
This is not quicksort and your partition function... doesn't partition. The variables are ill-named. There is no whitespace used to make the code more readable and the indentation is horrible and inconsistent.
Make your code readable. You might want to write out or refer to the algorithm before (and while) you attempt to code it because you're nowhere near a quicksort implementation.
hey madh
In my opinion, the quicksort's partition alogrithm is like:
1. Choose a number as pivot.
2. Throw numbers that samller than the pivot to the left, and the bigger ones to the right.
3. Repeat above steps till you only have 2 or 1 number in partition.
I didn't check these code, but i think it should do the explaination:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void partition(int *low, int *high)
{
int key;
key=*low;
while (low<heigh)
{
while ((low<high)&&(*high>key)) high--;
*low=*high;
while ((low<high)&&(*low<key)) low++;
*high=*low;
}
*low=key;
}
And i also think that a right implementation of an algorithm should be elegant, your first post was ,well, obviously not much elegant.(:
Anyway, cheer up!