UVa 110 - Meta-Loopless Sorts

ACM UVA 110 - Meta Loopless Sort
Meta Programming

Meta programming is a far effective programming technique to optimise the resolution of problems involving constants.

A example of meta programming is the sorting of long fixed size arrays which takes far less time and far more memory to be executed than a standard sorting algorithm. Such optimisations are useful while considering the size of the array invariant and several requests for sorting operations.

Futhermore, the time taken to sort such arrays can be reduced from $$O(n.log(n))$$ to $$O(n)$$ considering a memory addressing space of $$O(n.n!)$$. If several sortings are considered and n is small enough for a memory of $$O(n.n!)$$ to be arranged and great enough for $$O(n.log(n))$$ to be really bigger than $$O(n)$$, then a meta programming aproach might be compelling.

The technique used in this case is disposing the n! permutations of in any order and building over it a tree of comparisons to reach the desired permutations.

Generating Permutations

Many are the methods which can be used to generate the permutations of a sequence of symbols, one of which is described as follows:

A empty list is generated. Then the first element is insert. Then for each position in which the second element can be insert, the element is insert and the process is repeated for the thrid element, following the removal of the second one. In pseudo-code:

/* Have a list L generated and emptied. To generate all permutations of a the array A, call permutations(A, 0). permutations(array A, int a) { if (a < A.length) { foreach (i, 0, L.length) { L.insert(A[a], i); permutations(A, a+1); L.remove(i); } }  else { output(L); } }

Problem resolution

This problem is a simple implementation of the above algorithm.

To solve it, add a comparison clause for each insertion proceeded before the last element and go back one comparison level for each removal.

--Schultz 09:34, 25 Oct 2006 (EDT)