Skip to content

Instantly share code, notes, and snippets.

@hwayne
Last active September 17, 2022 00:10
Show Gist options
  • Save hwayne/3465527416060b6e60bf6eb0c369c233 to your computer and use it in GitHub Desktop.
Save hwayne/3465527416060b6e60bf6eb0c369c233 to your computer and use it in GitHub Desktop.
Bubblesort Algorithm
---- MODULE BubbleSort ----
EXTENDS Integers, TLC, Sequences, FiniteSets
Counter(seq) ==
LET
Range == {seq[x] : x \in DOMAIN seq}
CountOf(x) == Cardinality({i \in 1..Len(seq): seq[i] = x})
IN
[val \in Range |-> CountOf(val)]
IsSorted(seq) ==
\A i, j \in 1..Len(seq):
i < j => seq[i] <= seq[j]
(*--fair algorithm sort
(* github highlighting is borked, unbork it *)
variables
input \in [1..4 -> 1..4];
i = 1;
is_sorted = FALSE;
define
Correct == <>[](IsSorted(input))
SameInput == [][UNCHANGED Counter(input)]_input
end define;
begin
while ~is_sorted do
is_sorted := TRUE;
i := 1;
while i <= Len(input)-1 do \* -1 b/c we do input[i + 1]
if input[i] > input[i + 1] then
is_sorted := FALSE;
input[i] := input[i+1] || input[i+1] := input[i];
end if;
i := i + 1;
end while;
end while;
\* input[1] := 0;
end algorithm;*)
====
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment