all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,45 @@
DIM test(9)
test() = 4, 65, 2, 31, 0, 99, 2, 83, 782, 1
perms% = 0
WHILE NOT FNsorted(test())
perms% += 1
PROCnextperm(test())
ENDWHILE
PRINT ;perms% " permutations required to sort "; DIM(test(),1)+1 " items."
END
DEF PROCnextperm(a())
LOCAL last%, maxindex%, p%
maxindex% = DIM(a(),1)
IF maxindex% < 1 THEN ENDPROC
p% = maxindex%-1
WHILE a(p%) >= a(p%+1)
p% -= 1
IF p% < 0 THEN
PROCreverse(a(), 0, maxindex%)
ENDPROC
ENDIF
ENDWHILE
last% = maxindex%
WHILE a(last%) <= a(p%)
last% -= 1
ENDWHILE
SWAP a(p%), a(last%)
PROCreverse(a(), p%+1, maxindex%)
ENDPROC
DEF PROCreverse(a(), first%, last%)
WHILE first% < last%
SWAP a(first%), a(last%)
first% += 1
last% -= 1
ENDWHILE
ENDPROC
DEF FNsorted(d())
LOCAL I%
FOR I% = 1 TO DIM(d(),1)
IF d(I%) < d(I%-1) THEN = FALSE
NEXT
= TRUE