The tiniest C sort function? (2008)
cs.dartmouth.edu
cs.dartmouth.edu
Note: Enjoy discriminating between the one and the lower-case l.
subroutine hpsort(n,b)
integer b(500),bstar
n1=n
l=1+n/2
11 l=l-1
bstar=b(l)
goto 30
25 bstar=b(n1)
b(n1)=b(1)
29 n1=n1-1
30 l1=l
31 m=2*l1
if(m-n1)32,33,37
32 if(b(m+1).ge.b(m)) m=m+1
33 if(bstar.ge.b(m))goto 37
b(l1)=b(m)
l1=m
goto 31
37 b(l1)=bstar
if(l.gt.1)goto 11
if(n1.ge.2)goto 25
return
end(If they must, https://en.wikipedia.org/wiki/Fortran#Simple_FORTRAN_II_prog... is in error. It has label 601 follow label 799)
Of note is the statement
if(m-n1)32,33,37
That jumps to label 32 if (m-n1) is less than zero, to label 33 if it is zero, and to label 37 if it is larger than zero.Isn't it exponential? An n element sort calls two n-1 element sorts. Each of those calls two n-2 element sorts, and so on. So you get 2^n total calls, if I'm not mistaken. That is also why the page asks if you can beat it with a function of equal length but subexponential runtime.
for (i = j/2; j > 1; t[l] = k) {
if (i) k = t[i--]; else { k = t[j]; t[j--] = t[1]; }
for (l = i + 1; (m = l + l) <= j; t[l] = t[m], l = m) {
if (m < j && t[m] < t[m+1]) m++;
if (t[m] <= k) break;
}
}> Or for something completely different... x slides to 0.
while (x --\
\
\
\
> 0)
printf("%d ", x);The general field of endeavour for searching binary programs is called superoptimization[2] and people really do it for very small program fragments.
Making it hard for humans to read by removing white space is fun and all, but entirely pointless to the machine.