-
Notifications
You must be signed in to change notification settings - Fork 1
/
070-strange_sort_list.dfy
84 lines (81 loc) · 2.46 KB
/
070-strange_sort_list.dfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
method strange_sort_list_helper(s: seq<int>) returns (sorted: seq<int>, strange: seq<int>)
// post-conditions-start
ensures multiset(s) == multiset(sorted)
ensures |s| == |sorted| == |strange|
ensures forall i :: 0 <= i < |s| && i % 2 == 0 ==> strange[i] == sorted[i / 2]
ensures forall i :: 0 <= i < |s| && i % 2 == 1 ==> strange[i] == sorted[|s| - (i - 1) / 2 - 1]
// post-conditions-end
{
// impl-start
sorted := SortSeq(s);
strange := s;
strange := [];
var i := 0;
while i < |s|
// invariants-start
invariant 0 <= i <= |s|
invariant |strange| == i
invariant forall j :: 0 <= j < i && j % 2 == 0 ==> strange[j] == sorted[j / 2]
invariant forall j :: 0 <= j < i && j % 2 == 1 ==> strange[j] == sorted[|s| - (j - 1) / 2 - 1]
// invariants-end
{
if i % 2 == 0 {
strange := strange + [sorted[i / 2]];
} else {
var r := (i - 1) / 2; // 1 -> 0, 3 -> 1, 5 -> 2, 7 -> 3, ...
strange := strange + [sorted[|s| - r - 1]];
}
i := i + 1;
}
// impl-end
}
method strange_sort_list(s: seq<int>) returns (strange: seq<int>)
// post-conditions-start
ensures |s| == |strange|
// post-conditions-end
{
// impl-start
var _, s := strange_sort_list_helper(s);
strange := s;
// impl-end
}
method SortSeq(s: seq<int>) returns (sorted: seq<int>)
// post-conditions-start
ensures forall i, j :: 0 <= i < j < |sorted| ==> sorted[i] <= sorted[j]
ensures |sorted| == |s|
ensures multiset(s) == multiset(sorted)
// post-conditions-end
{
// impl-start
sorted := s;
var i := 0;
while i < |sorted|
// invariants-start
invariant 0 <= i <= |sorted|
invariant forall j, k :: 0 <= j < k < i ==> sorted[j] <= sorted[k]
invariant multiset(s) == multiset(sorted)
invariant forall j :: 0 <= j < i ==> forall k :: i <= k < |sorted| ==> sorted[j] <= sorted[k]
invariant |sorted| == |s|
// invariants-end
{
var minIndex := i;
var j := i + 1;
while j < |sorted|
// invariants-start
invariant i <= minIndex < j <= |sorted|
invariant forall k :: i <= k < j ==> sorted[minIndex] <= sorted[k]
// invariants-end
{
if sorted[j] < sorted[minIndex] {
minIndex := j;
}
j := j + 1;
}
if minIndex != i {
var temp := sorted[i];
sorted := sorted[i := sorted[minIndex]][minIndex := temp];
}
i := i + 1;
}
// impl-end
}