I have the following code for shell sort (recursive) given in the question
where t[ ] is an array to be sorted, n=no of elements initially. h is any large no initially,say h>n.
void shell_rec(int t[],int n,int h)
{
int aux,i;
if(h<=0)
return;
if(n>h)
{ shell_rec(t,n-h,h);
if(t[n]<t[n-h])
{
aux=t[n];
i=n;
for(i=n;i>=h && aux<t[i-h];i=i-h)
t[i]=t[i-h];
t[i]=aux;
}
}
shell_rec(t,n,h/3);
}
removing tail recursion i get:
void shell_rec(int t[],int n,int h)
{
int aux,i;
int j;
for(j=h;j>0;j=j/3)
{
if(n>j)
{ shell_rec(t,n-j,j);
if(t[n]<t[n-j])
{
aux=t[n];
i=n;
for(i=n;i>=j && aux<t[i-j];i=i-j)
t[i]=t[i-j];
t[i]=aux;
}
}
}
}
now i want to remove the recursion using explicit stack
pls help!