p = malloc(n * sizeof(int));
for (i = 0; i < n; i++)
p[i] = i;
output(p, n);
for (i = n - 1; i > 0; i--)
if (p[i] > p[i - 1])
{
for (j = n - 1; p[j] < p[i - 1]; j--);
swap(&(p[i - 1]), &(p[j]));
for (j = i, k = n - 1; j < k; j++, k--)
swap(&(p[j]), &(p[k]));
ouput(p, n);
i = n;
}
free(p);