-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathgnome_sort.rs
More file actions
38 lines (34 loc) · 856 Bytes
/
gnome_sort.rs
File metadata and controls
38 lines (34 loc) · 856 Bytes
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
extern crate creusot_std;
use creusot_std::{
logic::{Int, OrdLogic, Seq},
prelude::*,
};
#[logic(open)]
pub fn sorted_range<T: OrdLogic>(s: Seq<T>, l: Int, u: Int) -> bool {
pearlite! {
forall<i, j> l <= i && i < j && j < u ==> s[i] <= s[j]
}
}
#[logic(open)]
pub fn sorted<T: OrdLogic>(s: Seq<T>) -> bool {
sorted_range(s, 0, s.len())
}
#[ensures(sorted((^v).deep_model()))]
#[ensures((^v)@.permutation_of(v@))]
pub fn gnome_sort<T: Ord + DeepModel>(v: &mut Vec<T>)
where
T::DeepModelTy: OrdLogic,
{
let old_v = snapshot! { v };
let mut i = 0;
#[invariant(sorted_range(v.deep_model(), 0, i@))]
#[invariant(v@.permutation_of(old_v@))]
while i < v.len() {
if i == 0 || v[i - 1] <= v[i] {
i += 1;
} else {
v.swap(i - 1, i);
i -= 1;
}
}
}