>>> Continued from previous message
/******************************************************************/
int nat_merge (T *array,int n)
{
T *i,*j,*k,*l,*temp,*t_array;
int s,d,f;
if (NULL == (t_array = (T *) malloc (n * sizeof(T))))
return 0; /* Not enough heap available, fail cleanly */
s = 0; /* Sort from array to t_array */
do
{
if (s == 0)
{ /* Transfer from array to t_array */
i = array;
j = array + (n - 1);
k = t_array;
l = t_array + (n - 1);
}
else
{ /* transfer t_array to array */
k = array;
l = array + (n - 1);
i = t_array;
j = t_array + (n - 1);
}
d = f = 1; /* set increasing k and done flag */
while (i != j)
{
if (*i <= *j)
{ /* Start from left of source array */
*k = *i++; /* and transfer to destination */
k += d; /* move k in the appropriate direction */
if (*(i - 1) > * i) /* until the next element is out of sequence
*/
{
do
{
*k = *j--; /* then transfer from the other end */
k += d;
}
while (*(j + 1) <= *j); /* until out of sequence */
f = 0; /* clear completion flag, now store data */
d = -d; /* in reversed order to the last one */
temp = k; /* to the current position from the */
k = l; /* other end of the array */
l = temp;
}
}
else
{ /* start from right of source */
*k = *j--; /* and otherwise as above */
k += d;
if (*(j + 1) > * j)
{
do
{
*k = *i++;
k += d;
}
while (*(i - 1) <= *i);
f = 0;
d = -d;
temp = k;
k = l;
l = temp;
}
}
}
*k = *i; /* transfer the last element */
s = 1 - s; /* and swap array usage */
}
while (0 == f);
if (s) /* if data in t_array, move it to array */
for (i = array,j = t_array,s = 0;s < n;s++)
*i++ = *j++;
free (t_array); /* tidy up */
return 1; /* and show all went well */
}
/******************************************************************/
* SLMR 2.1a * Wastebasket: Something to throw things near.
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)
|