Well, I think I made it.
Code:
void quicksort_it(int *dataset,int l,int r)
{
stack<int> st;
int j;
st.push(r);
st.push(l);
while(!st.empty())
{
l=st.top();
st.pop();
r=st.top();
st.pop();
j=partition(dataset,l,r);
if(l < (j-1))
{
st.push(j-1);
st.push(l);
}
if((j+1) < r)
{
st.push(r);
st.push(j+1);
}
}
}
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);
return j;
}
void swap_in_array(int* array, int i, int j)
{
int tmp;
tmp=array[i];
array[i]=array[j];
array[j]=tmp;
}
I think this is correct.
All I need was a good sleep.