Hello, I'm implementing different sort algorithms as an excercise. However, I'm not sure how to implement quick sort algorithm.
Here's recursive implementation:
Code:
void quick_sort(int* array,int l,int r)
{
int j;
if(r<=l) return;
j=partition(array,l,r);
quick_sort(array,l,j-1);
quick_sort(array,j+1,r);
}
int partition(int* array, int l, int r)
{
int pivot,i,j;
pivot=array[l];
i=l+1;
j=r;
for(;;)
{
while((array[i] <= pivot) && (i <= r))i++;
while((array[j] > pivot) && (j > l))j--;
if(i < j)
swap_in_array(array,i,j);
else
break;
}
swap_in_array(array,j,l);
}
void swap_in_array(int* array, int i, int j)
{
int tmp;
tmp=array[i];
array[i]=array[j];
array[j]=tmp;
}
How to implement nonrecursive quick sort. I know I'll need stack,
and I manage to implement stack using single linked list.
Asssuming prototipes are:
void push(Stack* stack,int);
int pop(Stack*);
how code for quic sort would look like?
I don't need implamentation for stack, I've done that, but I need implementation for nonrecursive quick_sort.
I've searched the board, but found nothing to help me.
I hope you'll help me.
Thanks very much!