Files
pplus-library/pseudo/programs/10-insertion-sort.pseudo
alexm 568474d52f
Test (in pplus-core) / in-core (push) Failing after 28m44s
Initial commit
2026-11-12 22:23:48 -05:00

20 lines
550 B
Plaintext

// An in-place sort with a PROCEDURE and BYREF, plus a loop invariant.
// EXPECT: [1, 2, 3, 5, 8, 9]
PROCEDURE INSERTION_SORT(BYREF L : LIST OF INTEGER)
FOR I <- 1 TO LENGTH(L)
INVARIANT FORALL K IN [1, I) : L[K - 1] <= L[K]
DECLARE Key, J : INTEGER
Key <- L[I]
J <- I - 1
WHILE J >= 0 AND L[J] > Key
L[J + 1] <- L[J]
J <- J - 1
ENDWHILE
L[J + 1] <- Key
NEXT I
ENDPROCEDURE
DECLARE Xs : LIST OF INTEGER
Xs <- [5, 2, 9, 1, 8, 3]
CALL INSERTION_SORT(Xs)
OUTPUT Xs