Dafny Task 2 complete

This commit is contained in:
Aadi Desai 2022-11-21 13:51:44 +00:00
parent 30874e2405
commit 88e407c8f7
No known key found for this signature in database
GPG key ID: CFFFE425830EF4D9

View file

@ -34,13 +34,16 @@ method countsquares2(n:nat) returns (result:nat)
}
predicate sorted(A:array<int>)
reads A
reads A;
{
forall m,n :: 0 <= m < n < A.Length ==> A[m] <= A[n]
forall m,n :: 0 <= m < n < A.Length ==> A[m] <= A[n]
}
method binarysearch_between(A:array<int>, v:int, lo:int, hi:int) returns (result:bool)
requires false // Please remove this line.
requires sorted(A);
requires 0 <= lo <= hi <= A.Length;
decreases hi - lo;
ensures (exists j :: lo <= j < hi && A[j] == v) <==> result;
{
if lo == hi {
return false;
@ -58,14 +61,34 @@ method binarysearch_between(A:array<int>, v:int, lo:int, hi:int) returns (result
}
method binarysearch(A:array<int>, v:int) returns (result:bool)
requires false // Please remove this line.
requires sorted(A);
ensures (exists j :: 0 <= j < A.Length && A[j] == v) <==> result;
{
result := binarysearch_between(A, v, 0, A.Length);
result := binarysearch_between(A, v, 0, A.Length); // call with lo and hi to cover full array
}
method binarysearch_iter(A:array<int>, v:int) returns (result:bool)
requires sorted(A);
ensures (exists j :: 0 <= j < A.Length && A[j] == v) <==> result;
{
// Please provide an implementation of this method.
result := false;
var lo := 0;
var hi := A.Length;
while lo < hi
invariant 0 <= lo <= hi <= A.Length;
invariant forall j :: 0 <= j < lo ==> A[j] != v;
invariant forall j :: hi <= j < A.Length ==> A[j] != v;
decreases hi - lo;
{
var mid := (lo + hi) / 2;
if A[mid] > v {
hi := mid;
} else if A[mid] < v {
lo := mid + 1;
} else {
return true;
}
}
}
method partition(A:array<int>, lo:int, hi:int) returns (pivot:int)